总目录
布局就是layout文件中的一种标签,定义了一个容器。不同的布局有不同的特性。
1. 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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="World" /> </LinearLayout>
LinearLayout 声名这是一个LinearLayout android:layout_width 布局的宽度(这里是匹配整个父容器的宽度) android:layout_height 布局的高度(这里是匹配整个父容器的高度) android:orientation 容器的子元素的排列方式 android:gravity 容器子元素的对其方式 android:padding 容器的内边框 android:margin 容器的外边框 android:id 容器的id android:background 容器的背景颜色(使用16进制颜色) (注意:其他元素控件和布局也有某些和上述属性重合的属性)
放置在线性布局中的元素按照顺序排列,写在xml代码靠前的位置的控件在界面中排在上面或左边,上述例子中,Hello 与 World 分别排在上面和下面。
线性布局中的元素可以按照一定的权重排列,前提是设置控件宽度或高度为0dp。凡是设置宽度为0dp,其宽度由其权重决定。对于高度也一样。
<TextView android:layout_width="wrap_content" android:layout_height="0dp" android:text="Hello" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:text="World" android:layout_weight="2"/>上述例子中,第一个文本控件的权重设置为1,第二个为2,说明这两个文本控件的高度之和等于整个容器的总高度,且他们的高度比例是1:2。也就是说,两个文本控件分占据1/3 和 2/3 的父容器高度。宽度也一样。
android:layout_alignParentTop与父容器顶部对齐 android:layout_alignParentBottom与父容器底部对齐 android:layout_alignParentLeft与父容器左侧对齐 android:layout_alignParentRight与父容器右侧对齐 android:layout_centerHorizontal在父容器中水平居中 android:layout_centerVertical在父容器中垂直居中 android:layout_centerInParent在父容器中水平和垂直居中 android:layout_toLeftOf位于指定控件的左侧 android:layout_toRightOf位于指定控件的右侧 android:layout_above位于指定控件的上方 android:layout_below位于指定控件的下方 android:layout_alignTop与指定控件的顶部对齐 android:layout_alignBottom与指定控件的底部对齐 android:layout_alignLeft与指定控件的左侧对齐 android:layout_alignRight与指定控件的右侧对齐 android:layout_margin设置控件的外边距(上下左右) android:layout_marginTop设置控件的上外边距 android:layout_marginBottom设置控件的下外边距 android:layout_marginLeft设置控件的左外边距 android:layout_marginRight设置控件的右外边距 android:layout_width设置控件宽度(如 match_parent、wrap_content或固定值)android:layout_height设置控件高度(如 match_parent、wrap_content或固定值)
3. ContraintLayout(约束布局)
约束布局通过约束某个空间的顶部,左右,底部来确定空间的位置
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello ConstraintLayout!" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>上述代码指定文本控件的顶部约束于父容器的顶部,左端约束于父容器的左端......由于每个方向的约束等价,所以他们互相抵消,最终控件的位置处于父容器的正中央。当然,如果取消某个方向的约束,例如左端,控件会立即贴近父容器的右端,因为右约束的存在。当然,约束对象可以设置为其他控件,例如某个按钮约束于文本控件。
layout_constraintLeft_toLeftOf当前视图左侧与目标视图左侧对齐 layout_constraintTop_toTopOf当前视图顶部与目标视图顶部对齐 layout_constraintRight_toRightOf当前视图右侧与目标视图右侧对齐 layout_constraintBottom_toBottomOf当前视图底部与目标视图底部对齐 layout_constraintStart_toStartOf当前视图起始边与目标视图起始边对齐(考虑RTL布局) layout_constraintEnd_toEndOf当前视图结束边与目标视图结束边对齐(考虑RTL布局) android:layout_margin视图四边统一外边距 android:layout_marginStart视图起始边外边距 android:layout_marginTop视图顶部外边距 android:layout_marginEnd视图结束边外边距 android:layout_marginBottom视图底部外边距 layout_constraintHorizontal_bias水平方向偏移比例(0.0~1.0,默认0.5居中) layout_constraintVertical_bias垂直方向偏移比例(0.0~1.0,默认0.5居中) android:layout_width视图宽度(可设为 match_constraint以填充剩余空间)android:layout_height视图高度(可设为 match_constraint以填充剩余空间)layout_constraintWidth_min视图最小宽度(配合 match_constraint使用)layout_constraintHeight_min视图最小高度(配合 match_constraint使用)layout_constraintWidth_max视图最大宽度(配合 match_constraint使用)layout_constraintHeight_max视图最大高度(配合 match_constraint使用)layout_constraintHorizontal_chainStyle水平链条样式( spread、spread_inside、packed)layout_constraintVertical_chainStyle垂直链条样式( spread、spread_inside、packed)layout_constraintDimensionRatio视图宽高比例(如 16:9或H,16:9)layout_constraintGuide_begin辅助线距离父容器起始边的距离 layout_constraintGuide_end辅助线距离父容器结束边的距离 layout_constraintGuide_percent辅助线位置百分比(0.0~1.0) layout_constraintCircle目标视图ID(用于圆形定位) layout_constraintCircleRadius圆形定位半径 layout_constraintCircleAngle圆形定位角度(0~360度)