layout_weight详解示例



layout_wlayout_weight表示该View控件占据父控件剩余空间的比例

代码示例:

1.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<LinearLayout
		android:background="#0000FF"
		android:layout_height="match_parent"
		android:layout_width="match_parent" />
	<LinearLayout
		android:background="#00FF00"
		android:layout_height="match_parent"
		android:layout_width="match_parent" />
</LinearLayout>


未使用layout_weight,第一个linearLayout将父控件全部覆盖,无法显示第二个。

如果android:layout_height="wrap_content"android:layout_width="wrap_content"将导致两个linearLayout没有内容,显示在角落,无法看见。


2.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<LinearLayout
		android:background="#0000FF"
		android:layout_height="match_parent"
		android:layout_width="match_parent"
		android:layout_weight="1" />
	<LinearLayout
		android:background="#00FF00"
		android:layout_height="match_parent"
		android:layout_width="match_parent"
		android:layout_weight="1" />
</LinearLayout>

示例一

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<LinearLayout
		android:background="#0000FF"
		android:layout_height="0dp"
		android:layout_width="match_parent"
		android:layout_weight="2" />
	<LinearLayout
		android:background="#00FF00"
		android:layout_height="0dp"
		android:layout_width="match_parent"
		android:layout_weight="1" />
</LinearLayout>

示例二


对比示例一二可以很好的理解layout_weight的定义,即表示占据父控件剩余空间的比例;

示例一中,两个子线性布局的layout_weight均为1,所以每一个布局占据父控件剩余空间的1/(1+1)=0.5;

实例二中,LinearLayout1layout_weight2LinearLayout2layout_weight1,所以LinearLayout1占据父控件的比例是2/3LinearLayout2占据父控件的比例是1/3


3.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<TextView
		android:layout_height="wrap_content"
		android:text="small"
		android:layout_width="wrap_content"
		android:layout_weight="1" />
	<TextView
		android:layout_height="wrap_content"
		android:text="A very very long text that needs to wrap."
		android:layout_width="wrap_content"
		android:layout_weight="1" />
</LinearLayout>

实例三:注意TextView12均为android:layout_width="wrap_content"


因为TextView12根据weight属性分别占据父控件的1/2,所以宽度应该相同。但是结果并不是这样,TextView1宽度明显小于TextView2

这是因为layout_weight表示的并不是占据父控件的比例,而是父控件剩余空间的比例。TextView1TextView2内容填充完后,并没有完全布满父控件的宽度,还有剩余空间,即图中所示。根据layout_weight属性,两者分别占据了剩余空间的1/2.

TextView1TextView2的宽度并不相同,但是两个TextView除了内容外的空余部分确实相同的。

通过对比实例三和示例四,相信可以很好地理解剩余空间比例的问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<TextView
		android:layout_height="wrap_content"
		android:text="small"
		android:layout_width="0dp"
		android:layout_weight="1" />
	<TextView
		android:layout_height="wrap_content"
		android:text="A very very long text that needs to wrap."
		android:layout_width="0dp"
		android:layout_weight="1" />
</LinearLayout>

实例四


4.weightSum

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="#FF0000"
	android:weightSum="4"
	android:padding="5dp"> <!-- to show what the parent is -->
	<LinearLayout
		android:background="#0000FF"
		android:layout_height="0dp"
		android:layout_width="match_parent"
		android:layout_weight="2" />
	<LinearLayout
		android:background="#00FF00"
		android:layout_height="0dp"
		android:layout_width="match_parent"
		android:layout_weight="1" />
</LinearLayout>



在上面的示例中,父控件的weight根据将子控件的weight相加得到,所以父控件的空间被子控件完全填充;

使用weightSum,可以设置父控件的weight属性多于子控件,这样父控件不会完全被占满,其中一部分显示父控件的内容。



原英文作者网址http://www.chess-ix.com/blog/the-use-of-layout_weight-with-android-layouts/


作者的示例很好的体现了layout_weight的用法和意义,所以照搬过来。


解释部分是按照原文和自己的实践理解写的,有错误还望指出!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值