ScrollView ignores child's layout_height

2019-04-20 05:12发布

问题:

Got a LinearLayout containing a textview and an edittext. The LinearLayout is inside a ScrollView.

The ScrollView is inside another view hierarchy (part of a Work Space layout, like the Android Homescreen kinda), but this fills parent, both height and width (I set fill color different to check this).

With attributes of fill_parent for layout_height and layout_width on the LinearLayout though, it's only filling the parent width-ways, not height. Setting the height to fixed also doesn't work/ no effect. It's as if it's stuck on wrap_content, in effect.

In actuality, checking the hierarchy viewer, it's set to match_parent. Oddly, the weightsum of the LL is set to -1 (in the Hierarchy Viewer); the weightsum is not explicitly set.

If there's any further info I can supply to figure out why it's not honouring the layout_height attribute, let me know :)

Thanks in advance.

回答1:

ScrollView has an infinite height (or height determined by the needs of its child), so layout_height="fill_parent" is not meaningful for views inside it.

Edit - Romain Guy has an interesting article about this, and the solution is indeed to use fillViewport.

Try setting it on the ScrollView itself in xml: android:fillViewport="true"



回答2:

It sounds like a bit of a troublesome layout to me (a mess :P)

But what I would say is play with the fillViewPort attribute, which will make the ScrollView take up the height available to it Android fillViewPort Docs

If you however want something similar to home screen I found out a way to force the view to take that exact height (See the stock Android news app, Tweetdeck or Admob [beta] for the sliding effect I'm referring to).

If this is the case let me know and I'll bung up some source code to do it.