`

layout_weight的使用陷阱

阅读更多

<?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="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linear_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:padding="15dp"
            android:text="评论 "
            android:textColor="@color/six1"
            android:textSize="@dimen/twoTitle"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:id="@+id/tv_supportWish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/redClicked"
            android:gravity="center"
            android:padding="15dp"
            android:text="支持Ta"
            android:textColor="@color/white"
            android:textSize="@dimen/twoTitle" />
    </LinearLayout>

</LinearLayout>

看样子是左右平分了,其实没有,因为左右的文本不是占据一样的宽度的,左边的少两个字节

 

解决方案1

左边的文本改成   android:text="评论 \u3000",这样左右文本占用一样的字节,就会平分了

解决方案2

 android:layout_width="0dp"

  android:layout_weight="1",这样也平分了

<?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="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linear_share"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:padding="15dp"
            android:text="评论 "
            android:textColor="@color/six1"
            android:textSize="@dimen/twoTitle"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:id="@+id/tv_supportWish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/redClicked"
            android:gravity="center"
            android:padding="15dp"
            android:text="支持Ta"
            android:textColor="@color/white"
            android:textSize="@dimen/twoTitle" />
    </LinearLayout>

</LinearLayout>

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics