这几天想换份工作,四处奔波,到处碰壁!深刻感受到自身能力有限,需要多加学习!
今天我们来聊聊 Android 的五大UI布局
一、LinearLayout(线性布局)
二、RelativeLayout(相对布局)
三、AbsoluteLayout(绝对布局)
四、TableLayout(表格布局)
五、FrameLayout(帧布局)
一、LinearLayout(线性布局):这个布局通过android:orientation 来设置组件的对齐方式(垂直/水平)
下面我们来看代码
<span style="font-family:FangSong_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" //设置对齐方式为垂直
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" //设置内部组件为居中
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" //设置相对于父View对齐方式为右居中
android:text="button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
/>
</LinearLayout></span>
显示的效果
这里主要是android:gravity 和 android:layout_gravity 这两个属性的区别
android:gravity :这个是设置子组件的对齐方式,比如button组件可以用这个来设置button中text的对齐方式
android:Layout_gravity : 这个是设置组件相对于父组件的对齐方式,如代码中的Button1 用这个属性设置相对于父组件的右对齐
二、 RelativeLayout(相对布局):这个布局主要是通过组件的ID来设定相对的位置的显示,比如,现在我站在这里,然后你站在我的后面,这就是相对位置。
<1>. 属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 若找不到兄弟元素以父元素做参照物
<2>. 属性值必须为id的引用名”@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignBaseLine 该控件的baseline和给定ID的控件的Baseline对齐
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
<3>. 属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
如上面,就是一些基本的属性
<span style="font-family:FangSong_GB2312;font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="button1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:text="button2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="居中"
/>
</RelativeLayout></span>
三、AbsoluteLayout(绝对布局):这个布局通过使用 Layout_x 和 Layout_y 来准确地设置组件的xy坐标。同时允许组件有重叠。由于这个布局太过刚性,所以一般不建议使用。主要有下面两个属性
Layout_x x方向的坐标 Layout_y y方向的坐标
<span style="font-family:FangSong_GB2312;font-size:18px;"><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100"
android:layout_y="100"
android:text="button1"
/>
</AbsoluteLayout></span>
android:stretchColumns 伸展的列的索引
android:shrinkColumns 收缩的列的索引
android:collapseColumns 倒塌的列的索引(即销毁)
<span style="font-family:FangSong_GB2312;font-size:18px;"><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="3"
android:stretchColumns="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一列"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一列"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一列"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二列"
/>
</TableRow>
</TableLayout></span>
五、FragmentLayout(帧布局):最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象。比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前 一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。