android:layout_weight常见属性解析


1.常见的属性android:baselinerAligned

大家可以看到比例是1:2:3

可以看到,第一个TextView 和其他TextView对其方式并不是齐平的,也就是不符合我们的预期

那么为什么会发生这种现象呢!


先给大家化一条线

大家可以看到,相对于第一行文本而言他是对其的,虽然对于控件来说他们不是对其的。

那这是为什么呢?

TextView 会参考LinearLayout中的android:baselineraligned属性也就是基线。

那如何才能避免这个问题呢?

就会产生预期的效果。

2.假设我们把第一个宽度设置为android:layout_width="wrap_content",那么大家可以预期一下当前宽度是否还是1:2:3

现在看一下效果图


并不符合我们的预期,TextView1的宽度  明显>TextView2,也就是说并不是按照1:2:3来排序的

也就是说wrap_content 的确影响到了TextView在父控件所占据的宽度

那么这里说明了一个问题?


3.常见的属性android:weightsum:



<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="100dp"> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="16sp"/> <TextView android:id="@+id/author" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@color/gray"/> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@color/gray"/> <TextView android:id="@+id/need" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@color/gray"/> </LinearLayout> <com.google.android.material.button.MaterialButton android:layout_width="60dp" android:layout_height="100dp" android:layout_marginHorizontal="12dp" app:cornerRadius="6dp" android:textSize="20sp" android:text="报名" android:textStyle="bold"/> </LinearLayout> 如何解决Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值