设置android:layout_weight="1“时,有时会发现android:layout_width="0dp",有时则发现android:layout_width="wrap_content"
其实正确的做法是android:layout_width="0dp",因为width的优先级高于weight。可参看如下链接文章
http://blog.youkuaiyun.com/u010552788/article/details/24736405
另外一盘文章:
http://blog.youkuaiyun.com/xiechengfa/article/details/38334327
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1"
android:textColor="@android:color/white"
android:layout_weight="1"/> #当width=wrap_content时,权值作用为1:2:3,权值小则图像窄
<TextView
android:background="#cccccc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2"
android:textColor="@android:color/black"
android:layout_weight="2" />
<TextView
android:background="#ddaacc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3"
android:textColor="@android:color/black"
android:layout_weight="3" />
</LinearLayout>
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:background="#ff0000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
android:textColor="@android:color/white"
android:layout_weight="1"/> #此时权值小则比较宽
<TextView
android:background="#cccccc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2"
android:textColor="@android:color/black"
android:layout_weight="2" />
</LinearLayout>
本文探讨了Android中LinearLayout布局内元素如何使用layout_weight属性来分配空间。重点解释了android:layout_width=0dp与权重结合使用的正确方式,并通过实例展示了不同配置下视图的变化。
150

被折叠的 条评论
为什么被折叠?



