开发者

Linear layout (percentage definition - layout_weight) Android

1. Question

I would like to have a full screen linear layout with 2 lines, while the first line is divided into 2 sections, where one section occupies 30% and second 70% of the width and the second line is divided into 2 sections of the same size (each 50% of the width). Additionally the first line occupy only 33% of the screen height.

I think use of the android:layout_weight is the proper way. But it is little tricky as sometimes it applies to android:layout_width and sometimes to android:layout_height attribute and it applies to one or either depending on the values of respective XML attributes.

After experimenting I have found that if I set the attribute value to wrap_content it is overriden by android:layout_weight setting, e.g. following code applies the android:layout_weight to android:layout_width:

 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:background="#FF0000"/> 

Is this a correct approach?

2. Example

In order to demeaned layout (see image) I have to do this:

<LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#FF0000"/> 
开发者_开发知识库    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="2"
      android:background="#00FF00"/> 
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2">
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000FF"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000AA"/>      
  </LinearLayout>   
</LinearLayout>

Linear layout (percentage definition - layout_weight) Android

Thanks STeN


< LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

<LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#FF0000"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="2"
      android:background="#00FF00"/> 
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000FF"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000AA"/>      
  </LinearLayout>   
</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜