2012-10-15 68 views
8

Tôi có một bố cục xml nhập Mã PIN:Làm thế nào để tránh trọng số lồng nhau?

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FF000000" 
    android:orientation="vertical" 
    android:weightSum="1" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_margin="5dp" 
    android:layout_weight="0.2" 
    android:background="@android:drawable/edit_text" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/pinDisplay" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.85" 
     android:background="@null" /> 

    <ImageButton 
     android:id="@+id/backspace" 
     style="@android:style/Widget.Button" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_vertical" 
     android:layout_margin="3dp" 
     android:layout_weight="0.15" 
     android:gravity="center" 
     android:src="@android:drawable/ic_input_delete" /> 
</LinearLayout> 

<!-- Keypad section --> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.8" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_marginLeft="5dip" 
     android:layout_marginRight="4dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/one" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="1" 
      android:textAppearance="@android:attr/text" /> 

     <Button 
      android:id="@+id/two" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="2" /> 

     <Button 
      android:id="@+id/three" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="3" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0sp" 
     android:layout_marginLeft="5dip" 
     android:layout_marginRight="4dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/four" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="4" /> 

     <Button 
      android:id="@+id/five" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="5" /> 

     <Button 
      android:id="@+id/six" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="6" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0sp" 
     android:layout_marginLeft="5dip" 
     android:layout_marginRight="4dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/seven" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="7" /> 

     <Button 
      android:id="@+id/eight" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="8" /> 

     <Button 
      android:id="@+id/nine" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="9" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0sp" 
     android:layout_marginBottom="10dip" 
     android:layout_marginLeft="5dip" 
     android:layout_marginRight="4dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/ok" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:enabled="false" 
      android:text="OK" 
      android:textColor="#B8B8B8" /> 

     <Button 
      android:id="@+id/zero" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="0" /> 

     <Button 
      android:id="@+id/clear" 
      android:layout_width="0sp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Clear" /> 
    </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Việc bố trí là tốt, nhưng tôi nhận được rất nhiều lời cảnh báo Lint về trọng lượng lồng nhau.

trọng lồng nhau là xấu cho hiệu suất

tôi không thực sự chú ý đến tải bố trí chậm hoặc bất cứ điều gì. Tôi có thể làm gì để tránh trọng lượng lồng nhau? Hay tôi nên để nó như thế?

Trả lời

7

Bạn có thể sử dụng RelativeLayout để tránh trọng số lồng nhau.

Trọng lượng lồng nhau không tốt cho hiệu suất vì số lượng phép tăng tăng theo cấp số nhân với mỗi số được lồng nhau.

10

Nói chung, bạn nên cố gắng sử dụng RelativeLayout thay vì lồng nhiều LinearLayouts.

Từ documentation:

Lưu ý: Mặc dù bạn có thể làm tổ một hoặc nhiều bố trí trong một bố cục để đạt được thiết kế giao diện người dùng của bạn, bạn nên cố gắng để giữ cho hệ thống phân cấp bố trí của bạn càng cạn càng tốt. Bố cục của bạn vẽ nhanh hơn nếu bố cục có ít hơn bố cục lồng nhau (phân cấp chế độ xem rộng hơn tốt hơn so với phân cấp sâu cấu trúc phân cấp).

Nhưng khi bạn cần trọng lượng, như bây giờ, RelativeLayout sẽ không giúp bạn nhiều; thay vào đó, tôi khuyên bạn nên sử dụng TableLayout cho bàn phím của mình để giảm số lượng bố cục lồng nhau.

Các vấn đề liên quan