Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Percentage Layout Height

I know that it is impossible to set percentages and that you can set a weight of certain images to scale their heights. What I am trying to do though is specify the height of a layout relative to the layout it is within. Basicly I have something like this

<LinearLayout android:layout_height="fill_parent">   <LinearLayout>    </LinearLayout> </LinearLayout> 

Of course this is a very simplified version, just so you can understand my gibberish. Basicly I want to set the inner linearlayout to be around 50% of the main linearlayout.

What is the best way to do this?

like image 416
Somk Avatar asked Jun 01 '11 15:06

Somk


People also ask

How do you set the height of a view as a percentage in android?

You can set android:weightSum="2" in the parent linear_layout and android:weight="1" in the inner linear_layout. Remember to set the inner linear_layout to fill_parent so weight attribute can work as expected.

What is layout width and height in android?

android:layout_height. Specifies the basic height of the view. android:layout_width. Specifies the basic width of the view.

What is android Layout_weight?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.


1 Answers

There is an attribute called android:weightSum.

You can set android:weightSum="2" in the parent linear_layout and android:weight="1" in the inner linear_layout.

Remember to set the inner linear_layout to fill_parent so weight attribute can work as expected.

Btw, I don't think its necesary to add a second view, altough I haven't tried. :)

<LinearLayout     android:layout_height="fill_parent"     android:layout_width="fill_parent"     android:weightSum="2">      <LinearLayout         android:layout_height="fill_parent"         android:layout_width="fill_parent"         android:layout_weight="1">     </LinearLayout>  </LinearLayout> 
like image 93
Axel M. Garcia Avatar answered Sep 20 '22 13:09

Axel M. Garcia