Android中的LinearLayout布局

2025-05-29 0 32

LinearLayout : 线性布局

在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了, 线性布局是按照垂直方向(vertical)或水平方向(horizontal)的顺序依次排序子元素,每一个子元素都位于前一个元素之后,下面我们就简单的了解一下吧

在XML布局文件中会遇到如下一些单位

px:是屏幕的像素点 dp:一个基于density的抽象单位,屏幕的物理尺寸 sp:同dp相似,但会根据用户的字体大小进行缩放

XML代码如下:改变一下android:orientation="vertical" 垂直方向(vertical)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
<?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="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.administrator.adapter.MainActivity">

<TextView

android:text="第一个TextView"

android:background="#ef0808"

android:gravity="center"

android:textSize="18sp"

android:layout_width="match_parent"

android:layout_height="100dp" />

<TextView

android:text="第二个TextView"

android:gravity="center"

android:background="#31ef0b"

android:textSize="18sp"

android:layout_width="match_parent"

android:layout_height="100dp" />

<TextView

android:text="第三个TextView"

android:gravity="center"

android:textSize="18sp"

android:background="#ec07ca"

android:layout_width="match_parent"

android:layout_height="100dp" />

<TextView

android:text="第四个TextView"

android:gravity="center"

android:textSize="18sp"

android:background="#f5d105"

android:layout_width="match_parent"

android:layout_height="100dp" />

</LinearLayout>

运行结果:每一个TextView都从上往下依次排列

Android中的LinearLayout布局

XML代码如下:改变一下android:orientation="horizontal" 水平方向(horizontal)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
<?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="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.administrator.adapter.MainActivity">

<TextView

android:text="第一个TextView"

android:background="#ef0808"

android:gravity="center"

android:textSize="18sp"

android:layout_width="100dp"

android:layout_height="100dp" />

<TextView

android:text="第二个TextView"

android:gravity="center"

android:background="#31ef0b"

android:textSize="18sp"

android:layout_width="100dp"

android:layout_height="100dp" />

<TextView

android:text="第三个TextView"

android:gravity="center"

android:textSize="18sp"

android:background="#ec07ca"

android:layout_width="100dp"

android:layout_height="100dp" />

<TextView

android:text="第四个TextView"

android:gravity="center"

android:textSize="18sp"

android:background="#f5d105"

android:layout_width="100dp"

android:layout_height="100dp" />

</LinearLayout>

运行结果:每一个TextView都从左向右依次水平排列

Android中的LinearLayout布局

这两种线性布局唯一的差别就是android:orientation的值不同

实验总结:这两种线性布局唯一的差别就是android:orientation的值不同, 通过本次实验对Android中的线性布局有了一个初步的了解

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持快网idc!

原文链接:http://www.cnblogs.com/bigbossOne/p/6259937.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Android中的LinearLayout布局 https://www.kuaiidc.com/119525.html

相关文章

发表评论
暂无评论