Android(安卓)LinearLayout的android:layout_weight属性
16lz
2021-12-04
本文主要介绍Android LinearLayout的android:layout_weight属性意义
android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。
除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下
比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,
则比例为20%:50%:30%=2:5:3。layout代码如下
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:text="文本1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="5" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:text="文本2" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:text="文本3" /> </LinearLayout>
从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可
其中android:layout_alignParentLeft="true"android:gravity="center_vertical"是为了方便查看而设置
更多相关文章
- Android基于TextView属性android:ellipsize实现跑马灯效果的方法
- Android中RelativeLayout各个属性的含义
- Android(安卓)TextView属性详解
- android 布局属性详解
- Android(安卓)动画
- android gen文件不生成、R文件报错
- Android(安卓)利用drawable中的gradient属性实现背景颜色渐变
- 源码茶舍之android:externalService是什么属性?实现原理?
- RelativeLayout的一些布局属性