简体   繁体   中英

How to use android:layout_weight consistently for all screensizes

I have an app that uses android:layout_weight for header spacing, however when I view the app on a different screensize the spacing change (not aligned with the row data below).

I have 5 headers, but the headers don't align correctly for all devices.

How can I ensure I use android:layout_weight to achieve a common spacing layout that can work on all screensizes?

<LinearLayout
    android:id="@+id/listTitles"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/table_heading"
    android:paddingBottom="7dp"
    android:paddingTop="8dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="42dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.1"
        android:gravity="center"
        android:text="POS"
        android:textColor="#ffffff"
        android:textSize="12sp" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.8"
        android:gravity="left|center"
        android:text="NAME"
        android:textColor="#ffffff"
        android:textSize="12sp" />

     <TextView
        android:id="@+id/textView7"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.2"
        android:gravity="center"
        android:text="BIRTH"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.2"
        android:gravity="center"
        android:text="SCORE"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.4"
        android:gravity="center"
        android:text="POINTS"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.4"
        android:gravity="right|center"
        android:paddingRight="10dp"
        android:text="ROUND"
        android:textColor="#ffffff"
        android:textSize="12sp" />
 </LinearLayout>

There are two issues here:

  1. When using layout_weight , you must set layout_width to 0dp .

  2. The rows in your table or ListView must have the same layout_weight and layout_width values.

Set the parent LinearLayout weight_sum to 7 since all the TextView space occupied is equal to 7

Change the layout_width of all the TextView to 0dp. This is a modify version

<LinearLayout
    android:id="@+id/listTitles"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/table_heading"
    android:paddingBottom="7dp"
    android:weightSum="7"
    android:paddingTop="8dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.1"
        android:gravity="center"
        android:text="POS"
        android:textColor="#ffffff"
        android:textSize="12sp" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.8"
        android:gravity="left|center"
        android:text="NAME"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.2"
        android:gravity="center"
        android:text="BIRTH"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.2"
        android:gravity="center"
        android:text="SCORE"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.4"
        android:gravity="center"
        android:text="POINTS"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.4"
        android:gravity="right|center"
        android:paddingRight="10dp"
        android:text="ROUND"
        android:textColor="#ffffff"
        android:textSize="12sp" />
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM