【Android】线性布局(LinearLayout)最全解析
【Android】线性布局(LinearLayout)最全解析
一、LinearLayout概述
线性布局(LinearLayout)
主要以水平或垂直方式来排列界面中的控件。并将控件排列到一条直线上。在线性布局中,如果水平排列,垂直方向上只能放一个控件,如果垂直排列,水平方向上也只能方一个控件。
使用线性布局,需要将布局节点改成LinearLayout
,基本格式如下:
<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"
tools:context=".MainActivity">
....
</LinearLayout>
二、LinearLayout常用属性
2.1 orientation属性
在线性布局中,控件排列有水平和垂直两个方向,控件排列方向由android:orientation
属性来控制,该属性需要加在LinearLayout
标记的属性中。
从上图可以看出,将orientation
属性值设置成为horizontal
,控件将从水平方向从左往右
排列,将orientation
属性值设置成为vertical
控件将从垂直方向从上往下
排列。
2.2 gravity属性
线性布局的控件默认是从左往右
排列或从上往下
排列,如果想让线性布局中的控件排列对齐右边缘或者底部,可以用gravity
属性控制。
不过该属性值并不是只有在LinearLayout
中才能使用,其他的布局用该属性同样能生效。
2.3 layout_weight属性
LinearLayout
中另外一个常用的属性是layout_weight
,该属性需要加在LinearLayout
的子控件中。其作用是分配线性布局中的剩余空间到该控件上。
如下图所示,在控件没有添加layout_weight
属性时,控件未占满线性布局的区域会空出来。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
给控件button2加上android:layout_weight="1"
后,会把线性布局剩余空间全部占满。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
如果给button1
和button2
都加上android:layout_weight="1"
,则两个控件均匀分配剩余空间。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
如果给button1
加上android:layout_weight="3"
,button2
加上android:layout_weight="1"
,由于剩余的空间是200,button1
和button2
会按照3:1的比例来分配剩余空间。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="400dp"
android:layout_height="300dp"
android:background="@color/teal_200"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
相关文章
- Windows 11怎么下载安卓应用
- 微软激励奏效,OBS Studio 等上架Windows 11 应用商店
- 微软将改进 Windows 11 应用商店搜索算法,并支持浏览器网页端安装应用
- Windows 11首个预览版不支持运行安卓应用 微软回应:没取消、后续提供
- 去控制面板6年后:Windows 11仍搭载它
- JVM Sandbox入门教程与原理浅谈
- 使用Fiddler4实现手机抓包
- 供应链金融&区块链应用
- Hash函数及其应用
- VRAR概念的定义和要素以及技术定义和应用
- 苹果将封杀奖励用户启用ATT跟踪功能的应用程序
- iOS 15功能全面曝光,终于加入应用分屏?这回安卓系统领先了
- 盘点iOS 14.5最重要的5个更新:你的隐私由你掌管
- iOS 14.5 新功能,苹果详解:戴口罩时如何用 Apple Watch 解锁 iPhone
- iOS 14.5正式版终于来啦!十几个新功能都还挺实用
- 苹果iOS14.5正式版来了:用户升级后应用跟踪透明度无法使用
- 苹果推出具有争议性隐私功能的iOS 14.5
- fiddler 抓取手机app请求包
- APP接入和监管即将迎来最严新政,APP备案制也呼之欲出
- iOS的隐私跟踪到底要不要打开?看完就明白