2012-04-11 74 views
13

我有三个元素的布局,其方向是'水平'android weightSum无法正常工作

我想修复每个元素在我的布局中占用的大小。

第一个元素占总空间的40%,第二个元素占5%,第三个占据剩余55%的空间。

我的布局是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 


<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="100" > 

    <TextView 
     android:id="@+id/outletname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="40" 
     android:text="@string/outlet_name2" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:text=": " /> 

    <TextView 
     android:id="@+id/outletnamevalue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="55" 
     android:text="abcdefttt" /> 
</LinearLayout> 
</LinearLayout> 

文本“的TextView”会动态变化,所以我要继续为不论意见的内容元素不变的空间...

回答

43

在为了使重量有效,您还必须将TextViews设置为宽度0dp

android:layout_width="0dp" 
+10

完成答复,必须了解的是,重量仅适用于/ /剩余空间,即左一旦施加的layout_width值空间。 – njzk2 2012-04-11 09:53:03

+0

这使我的一天:D多少年...... LOL。所以这是问题的唯一答案。 UPVOTEEEEEEE – jace 2017-04-19 03:44:39

0

我使用以下公式:

layout_weight=(weightSum-PercentageYouWant*weightSum)/(TotalElements-1) 

第一元件应该采取的总空间的40%,则第二元件应该占据5%和第三个应占有剩余的55%的空间。

第1个要素:

(100-0.4*100)/(3-1)="30" 

第二个元素:

(100-0.05*100)/(3-1)="47.5" 

第三要素:

(100-0.55*100)/(3-1)="22.5" 

所以40% + 5% + 55% = 100%

layout_weight="30" + layout_weight="47.5" + layout_weight="22.5" = weightSum="100" 

这就是我的理解。

这里是一个例子(按钮只是用于图形参考)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="120" > 

    <!-- WeightSum=120 and I will add 4 elements, so TotalElements=4 --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="36" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 10%, so (120-10%*120)/(4-1)=36 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="10%" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="32" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 20%, so (120-20%*120)/(4-1)=32 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="20%" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="28" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 30%, so (120-30%*120)/(4-1)=28 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="30%" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="24" 
     android:orientation="vertical" > 

     <!-- I want this LinearLayout to be 40%, so (120-40%*120)/(4-1)=24 --> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="40%" /> 
    </LinearLayout> 

</LinearLayout>