1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android布局——LinearLayout 线性布局

Android布局——LinearLayout 线性布局

时间:2021-12-08 11:32:04

相关推荐

Android布局——LinearLayout 线性布局

简介

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

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

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

常用属性

属性

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

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

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

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

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

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

布局权重

在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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:gravity="center"tools:context=".MainActivity"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/purple_200"android:text="12345"/><TextViewandroid: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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:background="@color/purple_200"android:text="12345"/><TextViewandroid: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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:gravity="center"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/purple_200"android:text="111"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="2"android:background="@color/pink"android:text="222"/><TextViewandroid: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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="1111111"android:background="@color/teal_200" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="2222222"android:background="@color/purple_200" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="3333333"android:background="@color/white" /><TextViewandroid: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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:gravity="center"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:text="1111111"android:background="@color/teal_200" /><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:text="2222222"android:background="@color/purple_200" /><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:text="3333333"android:background="@color/white" /><TextViewandroid: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博客

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。