LinearLayout 线性布局
按从上到下, 或从左到右的顺序排列
<?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">
<!-- android:layout_weight="1"
权重是LinearLayout中布局的一个利器,值越大通常都是等其他布局完毕之后再将剩下的空间全部赋予有该属性的控件
如下 ImageView 和 LinearLayout布局就是权重的一个使用方面
-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/ad" />
<!--
权重的另一个使用 ,由于android设备的多样化,权重等比例在某些情况下特别有效。
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
-->
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="我是一个开心的Button按钮" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="我是一个开心的Button按钮" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="我是一个开心的Button按钮" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="12dp"
android:text="我是一个开心的Button按钮" />
</LinearLayout>
<!-- android:gravity 文字相对于控件的位置,比方说word中 文字左对齐 ,右对齐,居中对齐-->
<TextView
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="LONG LONG AGO, A Girl And A boy."
/>
</LinearLayout>
效果图:
RelativeLayout 相对布局
按照控件的相对关系 排列
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</RelativeLayout>
1.编写元素的时候, 可以指定它相对于父元素的位置。
2.编写元素的时候, 还可以指定相对于某个同级元素的位置。
> 主要的布局对其方式有三种:
1.1 在某个元素的上下左右: layout_toLeftOf, layout_above, layout_below
2.2 与某个元素的哪条边对其: layout_alignTop, layout_alignLeftOf...
3.3 与父类的边对其: layout_alignParentRight, layout_alignParentLeft
4.4 设置元素的边距: layout_margin, layout_marginLeft
FrameLayout 帧布局
一层盖一层
<FrameLayout>
<FrameLayout>
<ImageView src=""/>
</FrameLayout>
<FrameLayout>
<ImageView src=""/>
</FrameLayout>
<FrameLayout>
所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的控件被放在最底层,最后一个添加到框架布局
中的视图显示在最顶层,上一层的控件会覆盖下一层的控件,这个布局比较简单,简单来说就是堆积木,它的俯视图就是这个布局的效果
AbsoluteLayout 绝对布局(基本不用,过时的)
指定控件在Layout布局的绝对坐标位置x, y
//设置x坐标, y坐标
android:layout_x=""
android:layout_y=""
TableLayout 表格布局(基本不用)
类似Html表格
<TableLayout>
<TableRow>
</TableRow>
</TableLayout>
这些布局可以相互嵌套应用,合理的使用可以做出美观的界面。
常用的属性
> 第一类:属性值为true或false
android:layout_centerVertical 垂直居中
android:layout_centerHrizontal 水平居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名"@+id/idname"
android:layout_above 在某元素的的上方
android:layout_below 在某元素的下方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dp,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginTop 离某元素上边缘的距离
android:layout_marginRight 离某元素右边缘的距离
demo地址:Github
网友评论