五大布局
安卓五大布局提供一个放组件的容器。大家都知道秦国的蓝田大营,五钟布局相当于蓝田大营的房子,为士兵提供住所、睡觉、吃饭的地方。具体如下:
- LinearLayout
- RelativeLayout
- FrameJayout
- AbsoluteLayout
- TableLayout
一、Linearlayout—线性布局
线程布局,可以根据orientation属性进行排版设置。比如铺地板砖,你只能铺完前面一块,才能铺下面一块,而方向要么是横要么是竖。不能出现组件重合的情况。是XY轴进行布局。
比如:竖直排序,如下左图。
<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="80dp"
android:text="我是第一个组件"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="我是第二个组件"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="我是第三个组件"
android:gravity="center"/>
</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="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我是第一个组件"
android:gravity="center"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我是第二个组件"
android:gravity="center"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我是第三个组件"
android:gravity="center"
android:padding="5dp"/>
</LinearLayout>
RelativeLayout 相对布局
相对布局是可以分为:
- 根据父容器来约束子组件的屏幕位置,比如属性:layout_centerInParent
- 根据子组件的位置来确定当前组件的具体位置,比如属性:layout_below
相当于铺地板砖你可以在房子中间铺一个,在门铺一个,然后你可以根据第一个在它的下面铺,右面铺,斜对角铺。要铺的地板砖都是根据已铺的地板作为位置参照物,继续往下铺,这个布局可以出现组件重合的情况,可在XYZ轴布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我是第一个组件"
android:gravity="center"
android:padding="5dp"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/tv_two"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我是第二个组件"
android:gravity="center"
android:layout_below="@id/tv_one"
android:padding="5dp"/>
</RelativeLayout>
效果:
其他属性:
第一类:属性值为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 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/组件id”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如50dip,80px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
FrameLayout—-帧布局
帧布局都是覆盖布局,比如铺地板砖,如果不指定组件大小边距,都是一层一层的往上铺,相当于累地板,相当于XZ轴进行布局。比如:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_one"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="我是第一个组件"
android:gravity="center"
android:padding="5dp"
android:background="#bd0708"/>
<TextView
android:id="@+id/tv_two"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="我是第二个组件"
android:gravity="center"
android:padding="5dp"
android:background="#666"/>
</FrameLayout>
组件2覆盖了组件1
AbsoluteLayout—-绝对布局
绝对布局是具体制定了组价相对于父容器或者组件的距离。相当于铺地板时一款地板距离左墙多少,距离门多少,具体的数据来决定当前组件的位置。属性中:
android:layout_y=”50dp“
android:layout_x=”30dip“
这俩个属性是必须有的。严重缺乏灵活性。
TableLayout—-表格布局
表格布局是特殊的线性布局,在源码中,其实继承自LinearLayout:
public class TableLayout extends LinearLayout {
-----
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一排" />
<TableRow>
<TextView
android:id="@+id/tv_one"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#bd0708"
android:gravity="center"
android:padding="5dp"
android:text="我是第一个组件" />
<TextView
android:id="@+id/tv_two"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#666"
android:gravity="center"
android:padding="5dp"
android:text="我是第二个组件" />
<TextView
android:id="@+id/tv_three"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#999"
android:gravity="center"
android:padding="5dp"
android:text="我是第二个组件" />
</TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二排" />
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#bd0708"
android:gravity="center"
android:padding="5dp"
android:text="我是第一个组件" />
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#666"
android:gravity="center"
android:padding="5dp"
android:text="我是第二个组件" />
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#999"
android:gravity="center"
android:padding="5dp"
android:text="我是第二个组件" />
</TableRow>
</TableLayout>
效果:
至此,安卓的布局篇讲完。蓝田大营基地又一次建造了起来。接下来就是给房子装修了。
第一次用csdn markdown 写。不方便,很不方便,上网查询图片的缩放,目录的安插,一二级目录怎么使用。虽说有说明,但还是不好用。什么空格等,用原来的HTML编辑器,输入代码要删的话,有一行死活删不了,长时间不写了也不会用了。之前写的都是泛泛。没有深入的研究。现在根据自己所学,记录下自己的点点滴滴,每天记一点。知识垒一单。跬步集千里嘛。要是要啥问题,欢迎交流。