layout_weight的使用陷阱

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="    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="    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>

相关推荐