浅谈安卓布局之线性布局

来源:互联网 发布:手机吉他软件模拟 编辑:程序博客网 时间:2024/06/05 13:24

一、线性布局 linearlayout

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,

每一个元素都位于前面一个元素之后,若不指定默认水平。

1 android:orientation 确定线性布局的方向

vertical:表示垂直方向

horizontal:表示水平布局

2 android:layout_width 组件布局的宽度(也可自己定义大小)

macth_parent:填满parent的宽度(充满父控件)

wrap_content:组件宽度会依照内容大小而调整

3 android:layout_height 组件布局的高度

macth_parent:填满parent的高度(充满父控件)

wrap_content:组件高度会依照内容大小而调整

4 android:background:改变控件的颜色或在控件中添加图片,后面跟

#(xxxxxx)其中x代表字母或数字表示不同的颜色。

5 android:layout_margin:指定这个view距离上下左右的额外距离

其中还可指定某一方向上的距离:但是需要指定单位,

android:layout_marginLeft(左)

android:layout_marginRight(右)

android:layout_marginBottom(下)

android:layout_marginTop(上)

6 Layout_gravity:操控控件位于布局的什么位置。

left()right()top()bottom()center_vertical(位于垂直的中部)

center_horizontal(位于水平的中部)center(中部)

:当外部布局为垂直时,只能在水平方向上操作,当外部布局为水平时,

只能在垂直方向上操作。可以连写:android:layout_gravity="bottom|left"

Gravity:操控控件内部文字位于控件的位置。

left()right()top()bottom()center_vertical(位于垂直的中部)

center_horizontal(位于水平的中部)center(中部)

代码:

<Button 

       android:layout_gravity="bottom"

       android:layout_width="100dp"

       android:layout_height="100dp"

       android:text="btn1"/>

   <Button 

       android:gravity="bottom"

       android:layout_width="100dp"

       android:layout_height="100dp"

       android:text="btn2"/>

效果图:




0 0
原创粉丝点击