layout_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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.weight.MainActivity"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="111111111111" />
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="2"
        android:background="#4400ff00"
        android:gravity="center"
        android:text="2" />
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="3"
        android:background="#440000ff"
        android:gravity="center"
        android:text="3" />

</LinearLayout>


效果却是:




文字的底端是对齐的


想要得到预期的效果

需要加入:

android:baselineAligned="false"

效果如下图所示:




如果把第一个TextView的layout_weight属性设置为wrap_content

android:layout_width="wrap_content"


效果如下:


说明,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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.weight.MainActivity"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="111111111111" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="2"
        android:background="#4400ff00"
        android:gravity="center"
        android:text="2" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="2"
        android:background="#440000ff"
        android:gravity="center"
        android:text="3" />

</LinearLayout>

假设外层的LinearLayout的width为480


参考:首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配


那么剩余的尺寸是:rest = 480 - 480 * 3 = - 480 * 2


TextView 1 : 480 + ( - 480 * 2 / 5) = 480 * (3 / 5)

TextView 2, 3 : 480 + ( - 480 * 2 ) * ( 2 / 5 ) = 480 * (1 / 5 )


效果如下图所示:




(剩余的尺寸可以是负值)

控件宽度 + 父控件剩余宽度 * 比例



实现只有一个TextView且只占用屏幕的一半:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="horizontal" >
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="111111111111" />
    

</LinearLayout>

使用 android:weightSum="2"


效果如下:






你可能感兴趣的:(android,移动开发,layout_weight,控件布局,常见属性解析)