1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Andriod Studio 线性布局(LinearLayout)

Andriod Studio 线性布局(LinearLayout)

时间:2024-06-14 07:48:38

相关推荐

Andriod Studio 线性布局(LinearLayout)

Andriod有六种基本布局:线性布局LinearLayout、相对布局RelativeLayout、表格布局TableLayout、层布局FrameLayout、绝对布局AbsoluteLayout、网格布局GridLayout。但是在android开发中,频繁使用的是线性布局和相对布局,初学者学会这两种很重要,下面为大家介绍线性布局LinearLayout。

一、线性布局基本属性

android:id 标识符,即布局id,可以通过id来找到此布局或控件。但并不是要求每一个控件都要设置id属性android:layout_width 布局宽度android:layout_height 布局高度android:background 布局的背景,可以是颜色,图片,或者是自己自定义的一些文件android:layout_margin 外边距,用来设置布局或控件距离外部元素的边距android:layout_padding 内边距,用来设置布局或控件距离内部元素的边距android:orientation 布局方向,可以为设置水平或者竖直android:gravity 设置内部元素的对齐方式android:weight 权重,把剩余的空间按照权重比例去分配

二、属性简介和示例展示

1.android:layout_width 宽度

wrap_content:内容多大有多大

match_parent:匹配父控件,上一级控件多大就有多大

单位:dp(android单位)

2. android:orientation 方向

vertical:垂直

horizontal:水平

3.android:layout_padding 内边距

android:paddingLeft:左内边距

android:paddingRight右内边距

android:paddingTop:上内边距

android:paddingBottom:下内边距

示例展示

这里显示了黑色控件内部边距

android:paddingLeft="20dp"android:paddingRight="20dp"android:paddingTop="50dp"android:paddingBottom="10dp"

3. android:layout_margin 外边距

它同样具有左、右、上、下外边距下面直接展示

可以看见蓝框左、右有一段距离,即是它的外边距

android:layout_marginLeft="15dp"android:layout_marginRight="15dp">

4.android:gravity 对齐方式

示例展示

//居中对齐android:gravity="center"

// 水平居中android:gravity="center_horizontal"

// 靠底部排列android:gravity="bottom"

这里只例举了几种典型的对齐方式,其他的的大家可以自己去多多尝试

5. android:weight 权重

示例展示

//<view/>是所有控件的一个父类<Viewandroid:layout_width="50dp"android:layout_height="match_parent"android:background="#000000"android:layout_weight="1"/><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:background="#FF0033"android:layout_weight="1"/>

权重,把剩余的空间按照权重比例去分配,因为黑色控件本就占50dp的宽度,且红色和黑色控件的权重相同,所以剩下的空间会被平均分配,最终黑色的宽度会比红色大50dp

写到这儿相信大家对线性布局已经有了一定的了解,以后也会继续写一些关于Android开发的内容,方便一起学习交流,本文依据天哥的课程编写,大家有兴趣的可以去看看原视频

链接: /video/BV1Rt411e76H?p=3.

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