Android布局——LinearLayout 线性布局

简介

贴上官方文档:线性布局  |  Android 开发者  |  Android Developers

Linear 意为 线形的,线状的,所以该布局具有线的特点,要么是横向的,要么是竖向的。

该布局通过android:orientation (orientation:定位) 属性来设置布局内子控件是纵向还是横向,超过边界时,某些控件将消失。

常用属性

属性

android:orientation ——规定布局内子控件纵向还是横向

常量值 描述
horizontal 0 水平布局
vertical 1 垂直布局

android:layout_height ——指定该控件的的高度

android:layout_width ——指定该控件的宽度

注:该控件的值也可以自行设置常数

常量值 描述
match_parent -1 该控件大小和父级控件一样大
wrap_content -2 以该控件的内容为准的大小,即内容有多大就有多大

android:layout_weight ——指定该控件所占的权重 *

android:gravity ——指定控件内内容如何定位

常量值 描述
bottom 50 把对象堆到容器的底部,不改变大小
center 11

把对象放在容器的中心(水平垂直),不改变大小

center_horizontal 1 水平居中
center_vertical 10 垂直居中
fill 77 完全填满容器
left 3 推到容器左侧
right 5 推到容器右侧
top 30 推到容器顶部
start 800003 推到容器开头
end 800005 推到容器末尾

布局权重

在LinearLayout中可以用android:layout_weight来分配权重。此属性会根据视图应在屏幕上占据的空间大小,向视图分配“重要性”值。如果拥有更大的权重值,视图便可展开,填充父视图中的任何剩余空间。子视图可指定权重值,然后系统会按照子视图所声明的权重值比例,为其分配视图组中的任何剩余空间。默认权重为零。

均等分布

想要让每一个子视图都使用相同大小的空间,就必须把每个视图的android:layout_weight的值设置为1。

且如果是水平布局,则把每个视图的android:layout_width设为0dp;如果是垂直布局,则把每个视图的android:layout_height设为0dp。

示例如下:

水平布局

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/purple_200"
        android:text="12345"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:text="12345678910abcdefg"/>

</LinearLayout>

结果如图所示:

垂直布局

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/purple_200"
        android:text="12345"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:text="12345"/>

</LinearLayout>

结果如图所示:

不等分布 

 通过分配权重,权重数越大,代表该视图越重要,所占空间就越多。

如果有三个文本视图,左右两边声明为1,中间声明为2,则代表中间的视图更加重要,所占空间更多。

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/purple_200"
        android:text="111"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/pink"
        android:text="222"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:text="333"/>

</LinearLayout>

结果如图所示:

常用属性示例

 纵向布局

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1111111"
        android:background="@color/teal_200" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2222222"
        android:background="@color/purple_200" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3333333"
        android:background="@color/white" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="4444444"
        android:background="@color/purple_700" />

</LinearLayout>

 横向布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="1111111"
        android:background="@color/teal_200" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="2222222"
        android:background="@color/purple_200" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="3333333"
        android:background="@color/white" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="4444444"
        android:background="@color/purple_700" />

</LinearLayout>

参考博客:Android-0.Android Studio布局中layout_weight用法_花熊的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/TDSSS/article/details/126300680