Android学习——LinearLayout(线性布局)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengxu_kuangrexintu/article/details/82735566

前言

在实际的Andorid开发中,我们肯定经常使用LinearLayout(线性布局)。下面分享一下,我的学习心得。

LinearLayout(线性布局)

LinearLayout是一个视图组允许所有的子视图在竖直或水平单一方向上摆放的布局。

常用属性

1.android:orientation
设置布局管理器内组件的排列方式,可以设置为 horizontal (水平排列)、vertical (垂直排列)。

2.android:layout_width
设置布局管理器的宽度,可以设置为wrap_content(包裹大小)、match_parent(填充父控件)、指定大小(单位dp)。

3.android:layout_height
设置布局管理器的高度,可以设置为wrap_content(包裹大小)、match_parent(填充父控件)、指定大小(单位dp)。

4.android:gravity
设置布局管理器内组件的对齐方式,该属性值可设为 top(顶部对齐) 、bottom(底部对齐) 、left(左对齐) 、right(右对齐) 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直与水平方向都居中) 、 fill (填充)、 clip_vertical(垂直方向裁剪) 、 clip_horizontal(水平方向裁剪) 。
注意:可同时指定多种对其方式的组合,中间用“|”连接,如下方代码设置对齐方式为 left|center_vertical 表示出现在屏幕左边且垂直居中

5.android:layout_gravity
设置view在其父view中的对其方式。 该属性值可设为 top(顶部对齐) 、bottom(底部对齐) 、left(左对齐) 、right(右对齐) 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直与水平方向都居中) 、 fill (填充)、 clip_vertical(垂直方向裁剪) 、 clip_horizontal(水平方向裁剪) 。

6.android:padding
设置元素与边框之间的距离(外间距),这是一次设置四个方向。单独设置某个方向有另外四个属性分别是android:paddingTop(元素的上面)、android:paddingBottom(元素的下面)、android:paddingLeft(元素的左边)、android:paddingRight(元素的右边)。

7.android:layout_margin
设置相对于父控件之间的距离(内间距),这是一次设置四个方向。单独设置某个方向有另外四个属性分别是android:layout_marginTopandroid:layout_marginBottomandroid:layout_marginLeftandroid:layout_marginRight

8.android:layout_weight
设置控件在父布局的比重,通过百分百比来进行布局。需要注意的是,使用weight需要将android:layout_width或者android:layout_height属性设置为0或0px,不这样设置可以与你想要的效果不一样。

9.android:background
设置控件的背景,可以是图片或者颜色。

猜你喜欢

转载自blog.csdn.net/chengxu_kuangrexintu/article/details/82735566