LinearLayout 详解

2019-07-27  本文已影响0人  ImWiki

线性布局,是日常开发中最常用的布局,具备水平方向和垂直方式。本来是想几个布局一起讲解的,后来发现篇幅太长了,所以就单独介绍每一个布局的使用,并且伴随源码一起分析,这篇文章将会非常详细介绍LinearLayout,让我们充分了解LinearLayout的各种奇特的属性和功能,还有一些是我们平常很少使用的,充分了解后,我们可能会发现新大陆哦。

布局属性
Child View 布局属性

使用

基准线对齐

Boolean类型属性,如果等于true,那么就会开启以baseline作为对齐的方式,默认是true。如果设置了android:gravity="center_vertical",这个属性就失效了。

image.png
android:measureWithLargestChild

这个值是Boolean类型,如果是ture,该属性为true的时候, 所有带权重的子元素都会具有最大子元素的尺寸,但是使用是需要注意,父控件必须设置android:layout_width="wrap_content"

image.png
<?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="wrap_content"
              android:orientation="vertical">
    <TextView android:layout_margin="5dp"
              android:textColor="@android:color/black"
              android:text="android:measureWithLargestChild=true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
    <LinearLayout android:layout_margin="5dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:measureWithLargestChild="true">
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_red_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="AB"/>

        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_green_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="ABCDEF"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_blue_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="A "/>
    </LinearLayout>
    <TextView android:layout_margin="5dp"
              android:textColor="@android:color/black"
              android:text="android:measureWithLargestChild=false"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
    <LinearLayout
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_red_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="AB"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_green_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="ABCDEF"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_blue_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="A "/>
    </LinearLayout>
</LinearLayout>

分割线

分割线需要用drawable编写,当布局是水平方向,<size android:width起作用,决定分割线的宽度,android:dividerPadding作用于上下的padding。当布局是垂直布局,<size android:height起作用,决定分割线的宽度,android:dividerPadding作用于左右的padding。

image.png

linear_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="20dp" android:height="40dp"/>
    <solid android:color="@color/colorPrimary"/>
</shape>
<?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="wrap_content"
              android:orientation="vertical">

    <LinearLayout android:layout_margin="5dp"
                  android:showDividers="middle"
                  android:background="@android:color/holo_red_dark"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:divider="@drawable/linear_divider"
                  android:dividerPadding="5dp">

        <TextView android:textSize="30sp"
                  android:layout_width="wrap_content"
                  android:layout_height="50dp"
                  android:text="AB"/>

        <TextView android:textSize="30sp"
                  android:layout_width="wrap_content"
                  android:layout_height="50dp"
                  android:text="ABCDEF"/>
        <TextView android:textSize="30sp"
                  android:layout_width="wrap_content"
                  android:layout_height="50dp"
                  android:text="A "/>
    </LinearLayout>

    <LinearLayout android:layout_margin="5dp"
                  android:showDividers="middle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:background="@android:color/holo_red_dark"
                  android:divider="@drawable/linear_divider"
                  android:dividerPadding="50dp">
        <TextView android:textSize="30sp"
                  android:layout_width="wrap_content"
                  android:layout_height="50dp"
                  android:text="AB"/>

        <TextView android:textSize="30sp"
                  android:layout_width="wrap_content"
                  android:layout_height="50dp"
                  android:text="ABCDEF"/>
        <TextView android:textSize="30sp"
                  android:layout_width="wrap_content"
                  android:layout_height="50dp"
                  android:text="A "/>
    </LinearLayout>

</LinearLayout>

android:weight

如果设置该属性,那么和layout_height或layout_width就会有冲突,建议设置为0dp,当然不设置为0也有其意义,但是计算方式比较复杂,并不建议这么设置,也不好理解。

第1行:用于设定子控件占据LinearLayout的比例,这个属性非常实用,比如3个控件需要平局父控件,那么三个子控件都设置为1即可。

第2行:如果希望其他子控件正常显示,其中一个控件占据父控件剩余的空间,那么只设置这个控件为1即可。

第3行:android:weightSum是指所有带有android:weight的总和等于weightSum值,如果weightSum设置10,其中一个控件设置为5,那么代表这个控件占据剩余空间的1/2。

image.png
<?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="wrap_content"
              android:orientation="vertical">
    <LinearLayout android:layout_margin="5dp"
                  android:showDividers="middle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
        <TextView android:textSize="30sp"
                  android:layout_weight="1"
                  android:background="@android:color/holo_red_dark"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:text="AB"/>
        <TextView android:textSize="30sp"
                  android:layout_weight="1"
                  android:background="@android:color/holo_green_dark"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:text="ABCDEF"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_blue_dark"
                  android:layout_weight="1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:text="A "/>
    </LinearLayout>

    <LinearLayout android:layout_margin="5dp"
                  android:showDividers="middle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">

        <TextView android:textSize="30sp"
                  android:layout_weight="1"
                  android:background="@android:color/holo_red_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="AB"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_green_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="ABCDEF"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_blue_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="A "/>
    </LinearLayout>



    <LinearLayout android:layout_margin="5dp"
                  android:weightSum="10"
                  android:background="@android:color/holo_orange_light"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">

        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_red_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="AB"/>
        <TextView android:textSize="30sp"
                  android:background="@android:color/holo_green_dark"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="AB"/>
        <TextView android:textSize="30sp"
                  android:layout_weight="5"
                  android:background="@android:color/holo_blue_dark"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:text="AB"/>
    </LinearLayout>
</LinearLayout>
上一篇 下一篇

猜你喜欢

热点阅读