线性布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LayoutActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一组"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮3"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮4"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二组"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4"/>
效果
image
帧布局
所有的子控件都会在左上角,每个控件都会覆盖前面的控件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LayoutActivity">
android:layout_width="200dp"
android:layout_height="200dp"
android:text="我是1"
android:gravity="right|bottom"
android:textColor="#fff"
android:background="#123"/>
android:layout_width="150dp"
android:layout_height="150dp"
android:text="我是2"
android:gravity="right|bottom"
android:textColor="#fff"
android:background="#234"/>
android:layout_width="100dp"
android:layout_height="100dp"
android:text="我是3"
android:gravity="right|bottom"
android:textColor="#fff"
android:background="#456"/>
android:layout_width="50dp"
android:layout_height="50dp"
android:text="我是4"
android:gravity="right|bottom"
android:textColor="#fff"
android:background="#789"/>
效果图
image
相对布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LayoutActivity">
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相对布局"
android:textSize="20dp"/>
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_toLeftOf="@+id/btn2"
android:layout_alignTop="@+id/btn2"
android:layout_marginRight="20dp"/>
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_below="@+id/et"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"/>
效果:
image
可能会用到的
随便卡卡
android:layout_toLeftOf
该组件在引用组件的左边
android:layout_toRightOf
该组件在引用组件的右边
android:layout_above
该组件在引用组件的上边
android:layout_below
该组件在引用组件的下边
android:layout_alignParentLeft
对齐父组件的左边
android:layout_alignParentRight
对齐父组件的右边
android:layout_alignParentTop
对齐父组件的上边
android:layout_alignParentBottom
对齐父组件的下边
android:layout_centerInParent
相对于父组件居中
android:layout_centerHorizontal
横向居中
android:layout_centerVertical
垂直居中
表格布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1"
tools:context=".LayoutActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮6">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮7">
效果图
图片.png
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="2"
tools:context=".LayoutActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5">
效果图
图片.png
压缩和拉伸是按照0,1,2,3...排序的
网格布局
所有子控件默认在GridLayout中横向依次排列,当只等每行的列数时,到达指定列数
会自动换行显示
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
tools:context=".LayoutActivity">
android:text="0"
android:textColor="@android:color/black"
android:textSize="40dp"
android:layout_columnSpan="4"
>
android:text="清除"
android:textColor="@android:color/black"
android:textSize="26dp"
android:layout_columnSpan="4"
>
效果图:
图片.png
可能会用到的
随便卡卡
rowCount
几行
columnCount
几列
layout_rowSpan
合并行的个数
layout_columnSpan
合并列的个数
layout_column
在网格的第几列
layout_row
在网格的第几行