2012-04-11 17 views
13

Mam układ z trzech elementów, a jego orientacja jest „horyzontalne”android weightSum nie działa prawidłowo

Chcę ustalić wielkość, że każdy element zajmują w moim układzie.

Pierwszy element powinien zająć 40% całkowitej powierzchni, a drugi element powinien zajmować 5%, a trzeci zajmować pozostałe 55%.

Mój układ wygląda następująco:

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

tekst w „TextView” zmienia się dynamicznie, więc chcę, aby utrzymać stałą przestrzeń dla elementów, niezależnie od zawartości w widokach ...

Odpowiedz

43

W aby uczynić waga skuteczna, trzeba też trzeba ustawić szerokości swoich TextView do 0DP.

android:layout_width="0dp" 
+10

aby zakończyć odpowiedź, trzeba zrozumieć, że waga odnosi się tylko do/pozostałe/Przestrzeń, czyli miejsca w lewo raz została zastosowana wartość layout_width. – njzk2

+0

to sprawiło, że mój dzień: D przez ile lat ... LOL. więc to jedyna odpowiedź na problem. UPVOTEEEEEEE – jace

0

użyć wzoru:

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

pierwszy element powinna 40% całej powierzchni, a następnie drugi element powinny stanowić 5%, a trzecia powinny zajmują pozostałe 55% powierzchni.

1-ej:

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

2-ej:

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

3-ta Element:

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

tak 40% + 5% + 55% = 100%

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

Tak to rozumiem.

oto przykład (przyciski są tylko dla graficznego odniesienia)

<?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> 
Powiązane problemy