1

I have ads layout under the ScrollView. In order to prevent the ScrollView overlapping ads layout, I have to use

<ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" //          <--- THIS!
            >
                //...scrollview content
</ScrollView>

<include layout="@layout/ads_468x60"/>

What is the meaning of layout_weight if both width and height have been set to fill_parent?

According to documentation, this should not work, or to be precise, if both layout_height and layout_width have been set other than 0dp, then layout_weight is disregarded. But, in this example it works and bottom ads layout will not be shown without the attribute android:layout_weight="1" inside of the ScrollView.

sandalone
  • 41,141
  • 63
  • 222
  • 338

3 Answers3

3

use android:layout_height="0dp"

android:layout_weight="1" tells layout manager to fill all the free space.

Orest
  • 1,857
  • 5
  • 19
  • 27
  • You did not get. Even if I set this way, **the bottom ads layout IS visible** although it should not have been. Why is it still visible? Please try it yourself. The parent is LayoutView – sandalone May 17 '12 at 12:09
  • I'm sorry I don't understand what are you trying to achieve. You want to have scrollView that holds all free space in layout and some view(ads) in the bottom or what? – Orest May 17 '12 at 12:13
  • Yes. And I know how to do it. I do not understand why the upper code works when I haven't set layout_height to 0dp. – sandalone May 17 '12 at 13:25
  • 1
    because no matter what you set to android:layout_height, it is ignored so that layout_weight has higher priority. For example Eclipse give you an advice while creating xml that you SHOULD but not NEED to you use 0dp when setting android:layout_weight="1". You can explore ViewGroup sources...I suppose in onMeasure or in onLayout methods you find what you're interested about. – Orest May 17 '12 at 15:06
  • Upvote for mentioning priorities. It's a valuable info. Thanks. – sandalone May 17 '12 at 18:56
1

In your case, the layout_weight makes it ok to make your view smaller than defined (fill_parent) and it is shrinked to fit the screen space available. The weight indicates how to change the sizes of one or more components relatively to each other. In your case, you have just one view with weight. It is considered faster to use 0dp, as the system needs to measure less.

yoah
  • 7,180
  • 2
  • 30
  • 30
0

layout_weight is used for situations when you want to decide dynamically the width or height, so when you say layout_height = "0dp". It actually now depends on layout_weight attribute to make correct decision for it. So by setting up weightsum property on parent layout you can accordingly distribute weights on child layouts by providing layout_weight.

refer this guy's answer and description on layout_weight : Android Layout Weight

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66