layout_weight以及常见属性解析

本文通过四个实例详细解析了LinearLayout中layout_weight属性的使用方法及其显示效果的变化,并介绍了如何利用android:weightSum属性来精确控制各子元素所占的空间比例。

LinearLayout中的layout_weight属性,首先按照控件申明的尺寸进行分配,然后将剩下的尺寸按照weight分配

例一:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.listviewrefreshupdata.MainActivity">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_weight="1"
       android:gravity="center"
       android:background="#44ff0000"
       android:text="111111111111111111111111111"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_weight="2"
        android:background="#4400ff00"
        android:text="22222"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:gravity="center"
        android:background="#440000ff"
        android:text="3333333333"/>
</LinearLayout>
显示效果为:


例二:

将三个TextView的宽度都设置为match_parent

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.listviewrefreshupdata.MainActivity">

   <TextView
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:layout_weight="1"
       android:gravity="center"
       android:background="#44ff0000"
       android:text="11"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_weight="2"
        android:background="#4400ff00"
        android:text="2"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:gravity="center"
        android:background="#440000ff"
        android:text="2"/>
</LinearLayout>
显示效果为:


理由:


例三:

如果要实现如下效果:


那么可以设置父布局的

android:weightSum属性为2,然后设置textView的layout_weight为1
即:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2"
    tools:context="com.example.listviewrefreshupdata.MainActivity">

   <TextView
       android:layout_width="0dp"
       android:layout_height="50dp"
       android:layout_weight="1"
       android:gravity="center"
       android:background="#44ff0000"
       android:text="111111111111"/>

</LinearLayout>

例四:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.listviewrefreshupdata.MainActivity">

   <TextView
       android:layout_width="0dp"
       android:layout_height="50dp"
       android:layout_weight="1"
       android:gravity="center"
       android:background="#44ff0000"
       android:text="111111111111"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_weight="2"
        android:background="#4400ff00"
        android:text="2"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:gravity="center"
        android:background="#440000ff"
        android:text="2"/>
</LinearLayout>
预期效果应该是:


实际效果是:


为了达到预期效果,应该设置父布局的

android:baselineAligned="false"
即:

这样就可以达到预期效果了

总结:凡是以 "layout_"开头的属性都是由父控件去设置的属性,eg :layout_gravity:子控件相对父控件,gravity:子控件自身内容的相对位置



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值