布局指对界面结构的全面规划与安排,通过api中提供的各种布局能够快速的完成对于界面的设计
常用布局
线性布局(LinearLayout)
相对布局(RelativeLayout)
帧布局(FrameLayout)层层叠加
表格布局(TableLayout)
网格布局(GridLayout)
约束布局(ConstraintLayout)
添加布局
Java方法
setContentView()
例:
LinearLayout ll=new LinearLayout(this);//创建跟布局为线性布局的对象
setLayoutparams(new LinearLayout .Layoutparams(ViewGroup.Layoutparams.MATCH_PARENT,ViewGroup.Layoutparams.MATCH_PARENT))//设置宽高
ll.setBackgroundColor(Color.RED)//背景为红色
setContentView(ll)//制定此activity的内容视图为线性布局
xml方法
布局文件必须全小写可有数字但不能在开头也可有下划线
布局的重要属性
Android:layout_width宽度
Android:layout_height高度
match_parent匹配父容器
wrap_centent自适应内容大小
numberdp
Android:layout_padding内边距
Android:layout_margin外边距
linearlayout线性布局
线性布局重要属性(特有)
Android:orientation方向,horizontal水平(默认),vertical垂直
Android:layout_weight权重,控件在指定的方向上按照权重分配大小使用时将对应的宽高属性调为0dp
Android:layout_gravity重力偏向,偏向父容器那里bottom下,center中,top顶,left左,right右,center_horizontal水平居中,ceneter_vertical垂直居中,clip_horizontalclip_vertical
Android:gravity重力偏向内容偏向
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DCD7D7"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="36dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="<"
android:textColor="#ffffff"
android:textSize="40dp" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:text="一号鱼"
android:textSize="30dp"
android:layout_margin="13dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="#BFBEBE"
android:layout_weight="3"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#DCD7D7">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:layout_margin="3dp"
android:text="/@"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:gravity="bottom"
android:text="对方在等待海王的回复哦"
android:textSize="25dp" />
</LinearLayout>
</LinearLayout>
RelativeLayout相对布局
相对于父容器(取值:true/false)可搭配使用
android:layout_centerInParent="true"完全居中于父容器
ndroid:layout_alignParentLeft="true"左
ndroid:layout_alignParentRight="true"右
ndroid:layout_alignParentTop="true"顶
ndroid:layout_alignParentBottom="true"底部
android:layout_centerHorizontal="true"水平居中
android:layout_centerVertical="true"竖直居中
相对于其他控件(取值:其他控件的id)可搭配使用
android:id="@+id/idname"添加参照物id以便使用 参照物控件中添加
1在参照物的某边
android:layout_toLeftOf="@id/idname"在id的左边
android:layout_toRightOf="@id/idname"右边
android:layout_below="@id/idname"下面
android:layout_above="@id/idname"上面
2与参照物的边线对齐
android:layout_alignTop="@id/idname"上边线与上边线对齐
android:layout_alignBottom="@id/idname"下边线对齐
android:layout_alignLeft="@id/idname"左
android:layout_alignRight="@id/idname"右
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#A87AF858"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#BFAD11">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2D"
android:textSize="50dp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我上铺的兄弟"
android:textSize="45dp"
android:layout_alignParentBottom="true"
android:background="#2053CD"
></TextView>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#95730E"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#ff00ff"></RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#ff00aa"></RelativeLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F33576">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#137ED3"
></RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#043359"
></RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#137ED3"
></RelativeLayout>
</LinearLayout>
</LinearLayout>
帧布局(FrameLayout)层层叠加
android:foreground""前景
android:foregroundGravity前景重力
表格布局(TableLayout)
直接在TableLayout中添加控件,控件与父容器等宽如果想让控件在同一行,要在控件外层加一队<TableRow>中宽度默认为wrap_content
android:stretchColumns="0,1,2,3"可伸展的列也可用星*表示所有
android:shrinkColumns"0,2"可以收缩
android:collapseColumns隐藏列
网格布局(GridLayout)
android :rowCount行数量
android :columnCount列数量
android:layout_columnSpan="2"跨列占据两列但是并不会直接变化需要配合android:layout_gravity使用,或者配合宽
android:layout_rowSpan="2"跨行
约束布局(ConstraintLayout)
app:layout_constraintBottom_toBottomOf约束当前空间的底部位置left,right,top等
app:ayout_constraintHorizontal_bias水平偏移量0-1
app:ayout_constraintVertical_bias竖直偏移量