UI布局及其用法
主要分为六种 相对布局RelativeLayout 线性布局LinearLayout 表格布局TableLayout 网格布局GridLayout 帧布局FrameLayout 绝对布局AbsoluteLayout
其中 前三种较常用,下面列举前两种布局的属性以及一个案例,希望能对你们有用
1相对布局RelativeLayout
相对于兄弟元素 (控件与控件之间)
android:layout_below:在指定View的下方
android:layout_above:在指定View的上方
android:layout_toLeftOf:在指定View的左边
android:layout_toRightOf:在指定View的右边
相对于父元素
android:layout_alignParentLeft="true":在父元素内左边
android:layout_alignParentRight="true":在父元素内右边
android:layout_alignParentTop="true":在父元素内顶部
android:layout_alignParentBottom="true":在父元素内底部
对齐方式
android:layout_centerInParent="true":居中布局
android:layout_centerVertical="true":水平居中布局
android:layout_centerHorizontal="true":垂直居中布局
android:layout_alignTop:与指定View的上边界一致
android:layout_alignBottom:与指定View下边界一致
android:layout_alignLeft:与指定View的左边界一致
android:layout_alignRight:与指定View的右边界一致
间隔
android:layout_marginBottom离某元素底边缘的距离
android:layout_marginLeft离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
android:layout_paddingBottom 离父元素底边缘的距离
android:layout_paddingLeft离父元素左边缘的距离
android:layout_paddingRight离父元素右边缘的距离
android:layout_paddingTop离父元素上边缘的距离
关于margin 与padding 的区别问题
padding是站在父view的角度描述问题,是自己的内容与其父控件的边之间的距离。margin则是站在自己的角度描述问题,自己与旁边的某个组件的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。
2线性布局LinearLayout
有四个重要参数,直接决定了元素的布局和位置,。
android:layout_gravity :指定空间控件在布局中的对齐方式
android:gravity :指定文字在控件中的对齐方式
android:orientation :指的控件的方向。该属性的取值有vertical(垂直)和horizontal(水平)两种。vertical表示线性布局是垂直显示的,但只有水平方向的设置起作用。horizontal的表示水平显示,默认为horizontal。
android:layout_weight (线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大)
权重的作用:多个控件时,使控件平均分配。♦若为水平,添加android:layout_weight =”1”,把width改为0dp,height为match_parent。
♦若为垂直,添加android:layout_weight =”1”,把height改为0dp,width为match_parent。
例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#44BBBB"
android:layout_weight="1"
android:layout_margin="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#E6E61A"
android:layout_margin="5dp"
android:id="@+id/textView" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="#FF69B4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#E0EEE0"/>
</LinearLayout>
通过以上例子可以更好地理解权重的概念。
小编现在才初步学习UI布局,请多多担待。嘻嘻