2012-04-11 95 views
13

Tôi có bố cục có ba phần tử và hướng của nó là 'ngang'android weightSum không hoạt động đúng cách

Tôi muốn sửa kích thước mà mỗi phần tử chiếm trong bố cục của tôi.

yếu tố đầu tiên sẽ chiếm 40% tổng không gian, sau đó phần tử thứ hai phải chiếm 5% và phần thứ ba sẽ chiếm 55% không gian còn lại.

nhìn bố trí của tôi như thế này:

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

văn bản trong 'TextView' sẽ thay đổi tự động, Vì vậy, tôi muốn giữ không gian liên tục cho các yếu tố không phụ thuộc vào nội dung trong quan điểm ...

Trả lời

43

Trong để làm cho hiệu ứng trọng lượng hiệu quả, bạn cũng cần phải đặt độ rộng của TextViews thành 0dp.

android:layout_width="0dp" 
+10

để hoàn thành câu trả lời, người ta phải hiểu rằng trọng lượng chỉ áp dụng cho/còn/không gian, ví dụ: không gian còn lại khi giá trị layout_width đã được áp dụng. – njzk2

+0

điều này làm cho ngày của tôi: D trong bao nhiêu năm ... LOL. vì vậy đó là câu trả lời duy nhất về vấn đề này. UPVOTEEEEEEE – jace

0

tôi sử dụng công thức này:

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

yếu tố đầu tiên nên lấy 40% tổng số không gian sau đó Yếu tố thứ hai nên chiếm 5% và một phần ba nên chiếm còn lại không gian 55%.

Yếu tố 1:

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

2 phần tử:

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

3 Element:

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

nên 40% + 5% + 55% = 100%

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

đó là cách tôi hiểu.

đây là một ví dụ (nút chỉ là để tham khảo đồ họa)

<?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> 
Các vấn đề liên quan