0

I am designing an Android interface and I discovered I can give percentage to the control as Weight.

But when I use the following code :

 <LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="310.1dp"
    android:layout_weight=".70"
    android:id="@+id/linearLayout1"
    android:background="#C49268">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1" />
</LinearLayout>
<RelativeLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight=".30"
    android:layout_gravity="bottom"
    android:id="@+id/relativeLayout1">

The first layout is taking 70% of the page : android:layout_weight=".70"

The second layout is taking 30% of the page : android:layout_weight=".30" and a bottom gravity. So it should use the whole page. (100%).

What I understand from this is that the RelativeLayout should take 30% of the bottom space and the LinearLayout should take 70% of the top space for a total of 100% of the screen.

When I boot the app, I still see a black bar at the bottom (unused space). Am I doing something wrong ?

phadaphunk
  • 12,785
  • 15
  • 73
  • 107

3 Answers3

2

The main thing to keep in mind about layout_weight is that it distributes the REMAINING space. So, you should be setting whichever dimension (height or width) to "0dp" if you want to distribute things correctly.

In your case, you should be setting the layout_height to 0dp for both of your layouts

cheezy
  • 446
  • 1
  • 5
  • 17
  • I missed the part where it is attributing remaining space. So I could set everything to 0dp and wack percentages everywhere? – phadaphunk Dec 31 '13 at 19:56
  • Ah, found it. Here is the page that explained it really well for me. It demonstrates the differences between setting to 0dp and not. You'll want to set both `android:layout_weight` and `android:layout_height=0dp` for your `LinearLayout` and `RelativeLayout` [link](http://rlaenen.blogspot.com/2012/04/android-layoutweight-attribute-how-to.html) – cheezy Dec 31 '13 at 20:04
  • My bad, this is the correct one I was searching for (had bookmarked both). It's google cached since the page seems to be down now :( [link](http://webcache.googleusercontent.com/search?q=cache:1iXetZu-4WsJ:ugiagonzalez.com/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/+&cd=8&hl=en&ct=clnk&gl=us) – cheezy Dec 31 '13 at 20:19
0

try changing layout height to 0dp for both layouts and it should work

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
srbyk1990
  • 411
  • 5
  • 17
-2

change layout_width="fill_parent" and give it a background color. That will fix your problem.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
DjP
  • 4,537
  • 2
  • 25
  • 34