2011-08-03 60 views
0

1.问题线性布局(百分比定义 - layout_weight)的Android

我想有两行全屏幕布局线性,而第一行被分为2个部分,其中一个部分占据30 %和第二个70%的宽度和第二行被分成2个相同大小的部分(每个宽度的50%)。另外,第一行只占屏幕高度的33%。

我认为使用android:layout_weight是正确的方法。但它有点棘手,因为它有时适用于android:layout_width有时到android:layout_height属性,它适用于一个或者根据各自的XML属性的值。

实验我已经发现,如果我的属性值设置为WRAP_CONTENT它是由机器人重写后:layout_weight设置,例如下面的代码应用安卓layout_weight的android:layout_width

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

这是一个正确的做法?

2。实施例

为了贬低布局(参见图)我不得不这样做:

<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> 

enter image description here

由于 斯登

回答

1
< 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>