2012-04-11 87 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>