Android五大布局之一线性布局(LinearLayout)

一.LinearLayout(线性布局)重点属性:

android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局(ps:线性布局的核心,没有是会报错的)

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)是控制控件布局对齐方式的

(ps:可以同时使用多个参数,需要用“ | ”符号分隔开,比如在TextView控件里的文本需要靠在右上角就要添加以下属性android:gravity="top|right")

二.LinearLayout(线性布局)其他相关属性:

android:layout_gravity与android:gravity的区别:

android:gravity="bottom|right"(是本元素所有子元素的对齐方式,设置在父元素上)

android:layout_gravity (子元素在父元素的对齐方式,设置在子元素上)

当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用,即:left,right,center_horizontal 是生效的
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用,即:top,bottom,center_vertical 是生效的

android:padding="10dp" (是本元素所有子元素的与父元素边缘的距离,设置在父元素上)

android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上)

android:orientation (线性布局以列或行来显示内部子元素)

android:layout_weight ="1"(线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大。

三.例子(主要呈现出垂直水平布局以及控件内的文本方向)

1.首先我们先创建一个LinearLayout的XML文件

代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <LinearLayout 
 8         android:layout_width="match_parent"
 9         android:layout_weight="1"
10         android:layout_height="match_parent"
11         android:orientation="vertical" >
12         
13         <TextView 
14             android:layout_width="match_parent"
15             android:layout_height="wrap_content"
16             android:text="第一行"
17             android:background="#00ffff"
18             android:gravity="left"
19             android:layout_weight="1"
20             />
21         <TextView 
22             android:layout_width="match_parent"
23             android:layout_height="wrap_content"
24             android:gravity="right"
25             android:text="第二行"
26             android:background="#00ff00"
27             android:layout_weight="1"
28             />
29         <TextView 
30             android:layout_width="match_parent"
31             android:layout_height="wrap_content"
32             android:text="第三行"
33             android:background="#ffff00"
34             android:layout_weight="1"
35             />
36         <TextView 
37             android:layout_width="match_parent"
38             android:layout_height="wrap_content"
39             android:text="第四行"
40             android:gravity="bottom"
41             android:layout_weight="1"
42             android:background="#ff0000"
43             />
44     </LinearLayout>
45     
46     
47     <LinearLayout 
48        android:layout_width="match_parent"
49        android:layout_height="match_parent"
50        android:layout_weight="1"
51        android:orientation="horizontal" >
52        
53         <TextView 
54             android:layout_width="wrap_content"
55             android:layout_height="match_parent"
56             android:background="#ff0000"
57             android:text="第一列"
58             android:gravity="center_horizontal|center_vertical"
59             android:layout_weight="1"
60             />
61         <TextView 
62             android:layout_width="wrap_content"
63             android:layout_height="match_parent"
64             android:background="#ffff00"
65             android:gravity="bottom|center_horizontal"
66             android:text="第二列"
67             android:layout_weight="1"
68             
69             />
70         <TextView 
71             android:layout_width="wrap_content"
72             android:layout_height="match_parent"
73             android:background="#00ff00"
74             android:gravity="left|center_horizontal"
75             android:text="第三列"
76             android:layout_weight="1"
77             />
78         <TextView 
79             android:layout_width="wrap_content"
80             android:layout_height="match_parent"
81             android:background="#00ffff"
82             android:gravity="center_horizontal|center_vertical|right"
83             android:text="第四列"
84             android:layout_weight="1"
85             />
86     </LinearLayout>
87     
88     
89 
90 </LinearLayout>

运行结果如下:

以上就是我对LinearLayout(线性布局)理解

原文地址:https://www.cnblogs.com/zhaoyucong/p/6089557.html