首页 > 其他 > 详细

layout_weight的使用陷阱

时间:2015-06-07 02:10:57      阅读:346      评论:0      收藏:0      [点我收藏+]

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

layout_weight的使用陷阱

原文:http://wuxifu001.iteye.com/blog/2217502

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!