Android布局——LinearLayout 线性布局

简介

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

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

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

常用属性

属性

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

常量值描述
horizontal0水平布局
vertical1垂直布局

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

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

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

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

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

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

常量值描述
bottom50把对象堆到容器的底部,不改变大小
center11

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

center_horizontal1水平居中
center_vertical10垂直居中
fill77完全填满容器
left3推到容器左侧
right5推到容器右侧
top30推到容器顶部
start800003推到容器开头
end800005推到容器末尾

布局权重

在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博客

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TDSSS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值