LinearLayout线性布局
所有的子元素都按照垂直或水平的顺序在界面上排列
- 如果垂直排列,则每行仅包含一个界面元素
- 如果水平排列,则每列仅包含一个界面元素
LinearLayout元素的XML属性
属性名 |
属性值 |
备注 |
layout_width layout_height |
fill_parent match_parent |
布局元素的宽度/高度占满父元素的宽度/高度空间 |
wrap_content |
布局元素的宽度/高度为其内容宽度/高度 |
|
数值 |
数值表示的距离单位 |
|
orientation |
关键字 |
布局方向,线性水平布局或垂直布局 |
layout_weight |
数值 |
使用在View控件中,表示当前 LinearLayout剩余空间在View控件中的分配情况 |
xml创建布局
<?xml version="1.0" encoding="utf-8"?>
<!--ViewGroup可嵌套ViewGroup
android:orientation:设置子元素排列方式
horizontal:表示水平排列
vertical:表示垂直排列-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1