文章目录
LinearLayout :线性布局android:layout_gravity :控件的对齐方式android:layout_weight:权重RelativeLayout :相对布局相对于父布局进行定位相对于控件进行定位边缘对齐FrameLayout :帧布局Percent :百分比布局ConstraintLayout :约束布局自定义控件封装复用的页面引入封装好的布局自定义控件LinearLayout :线性布局
线性布局有水平、垂直两种排列方式:
android:orientation="vertical"
:垂直方向排列,此时高度不可被指定为match_parent
。android:orientation="horizontal"
:水平方向排列,此时不能将宽度指定为match_parent
。
android:layout_gravity :控件的对齐方式
如果布局方式选择horizontal
,之后设置button1
为top
;button2
为center_vertical
;button3
为bottom
。那么呈现效果如下:
android:layout_weight:权重
vertical
垂直布局时,layout_weight
可以覆盖layout_height
属性,根据权重来分配控件高度:
PS:通过上图应该对“android:orientation="vertical"
:垂直方向排列,此时高度不可被指定为match_parent
。”这句话有了深刻了解,match_parent
属性会导致控件占满整个屏幕……
horizontal
水平布局时,layout_weight
可以覆盖layout_height
属性,根据权重来分配控件高度:
RelativeLayout :相对布局
通过相对定位的方式可以使控件出现在布局的任何位置。
相对于父布局进行定位
关于位置的属性:
layout_alignParentLeft:处于父布局的左。layout_alignParentTop:处于父布局的上。layout_alignParentRight: 处于父布局的右。layout_alignParentBottom:处于父布局的下。layout_centerInParent:处于父布局的居中。
相对于控件进行定位
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".SecondActivity"><Buttonandroid:id="@+id/button_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button_3"android:layout_toLeftOf="@id/button_3"android:text="Button 1"/><Buttonandroid:id="@+id/button_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button_3"android:layout_toRightOf="@id/button_3"android:text="Button 2"/><Buttonandroid:id="@+id/button_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Button 3"/><Buttonandroid:id="@+id/button_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button_3"android:layout_toLeftOf="@id/button_3"android:text="Button 4"/><Buttonandroid:id="@+id/button_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button_3"android:layout_toRightOf="@id/button_3"android:text="Button 5"/></RelativeLayout>
layout_above:处于被引用控件之上。layout_below:处于被引用控件之下。layout_toLeftOf:处于被引用控件之左。layout_toRightOf:处于被引用控件之右。运行结果:
通过android:layout_centerInParent
定位button 3
之后,以其为基准,定位其他四个button
的位置。
边缘对齐
layout_alignLeft:该控件左边缘与被引用控件左边缘对齐。layout_alignRight:该控件右边缘与被引用控件右边缘对齐。layout_alignTop:该控件顶部边缘与被引用控件顶部边缘对齐。layout_alignBottom:该控件底部边缘与被引用控件底部边缘对齐。FrameLayout :帧布局
这种布局没有方便的定位方式,所有的控件都默认的摆放在布局的左上角。但可以类似于LinearLayout中通过layout_gravity
来指定控件在布局中的对齐方式:
Percent :百分比布局
layout_weight
属性让设计布局变得更方便,但可惜的是只有LinearLayout支持该功能,因此提供了PercentFrameLayout和PercentRelativeLayout分别解帧布局和相对布局的功能局限性。
具体来说,即可以不再使用wrap_content
和match_parent
等方式来指定控件大小,而是直接指定控件在布局中所占的百分比。
使用时,由于 Android 将百分比布局定义在了support
库中,因此只需在app/build.gradle
文件中添加下面依赖,需要注意的是 support 库在 Androidx 1.0.0 及更高版本中被AndroidX
库完全取代了……因此添加依赖时需如此实现:
只用完整路径androidx.percentlayout.widget.PercentFrameLayout
作为标签名,因为百分比布局不像其他三个内置在系统中。必须定义一个命名空间 app 才能使用百分比布局的自定义属性。使用layout_widthPercent
和layout_heightPercent
两个属性来定义控件长款,值以百分比形式表示。继承自FrameLayout,因此所有控件默认摆放在左上角,可以借助layout_gravity
来避免控件重叠。
ConstraintLayout :约束布局
常被视作增强型的相对布局,ConstraintLayout不仅可以解决LinearLayout常有的嵌套布局缺陷,还具备RelativeLayout的相对布局功能。
自定义控件
所有控件都是直接或者间接地继承自View的,所有布局都是直接或间接继承自ViewGroup的。View是 Android 中最基本的一种 UI 组件,它可以在屏幕上绘制一块矩形区域,响应这块区域的各种事件,封装好的各种控件其实就是在View的基础之上添加了各自特有的功能。ViewGroup是一种特殊的View,可以包含很多的子View和子ViewGroup,是一个放置控件和布局的容器。封装复用的页面
在前端页面中有许多重复使用频率高的页面,如导航栏、底部栏等,对于这些页面,可以一次编撰代码并封装,之后多次调用以实现复用。
这里通过约束布局实现标题栏布局文件title.xml
:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/cmy4"><Buttonandroid:id="@+id/title_back"android:layout_width="0dp"android:layout_height="50dp"android:layout_margin="5dp"app:layout_constraintHorizontal_weight="1"android:background="@drawable/cmy1"android:text="Back"android:textColor="#fff"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/title_text" /><TextViewandroid:id="@+id/title_text"android:layout_width="0dp"android:layout_height="wrap_content"android:gravity="center"android:text="Title Text"app:layout_constraintHorizontal_weight="2.5"android:textColor="@color/teal_200"android:textSize="24sp"app:layout_constraintTop_toTopOf="@id/title_back"app:layout_constraintBottom_toBottomOf="@id/title_back"app:layout_constraintLeft_toRightOf="@id/title_back"app:layout_constraintRight_toLeftOf="@id/title_edit" /><Buttonandroid:id="@+id/title_edit"android:layout_width="0dp"android:layout_height="50dp"android:layout_margin="5dp"app:layout_constraintHorizontal_weight="1"android:background="@drawable/cmy1"android:text="Edit"android:textColor="@color/white"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toRightOf="@id/title_text"app:layout_constraintRight_toRightOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
android:background 不生效
在res/values/themes.xml
中:
修改为:
相对定位
通过形如layout_constraintTop_toTopOf
的属性来定位控件,该类属性值可为parent
从而与父布局相适配。举两个例子,上述代码中:
title_back
中app:layout_constraintLeft_toLeftOf="parent"
:意为将title_back
的左边缘约束到父布局
的左边缘。title_edit
中app:layout_constraintStart_toEndOf="@id/title_back"
:意为title_edit
的起始位置即title_back
的结束位置。
相对定位中的layout_constraintBaseline_toBaselineOf
意为文本基线对齐。
对齐前: 对齐后:
通过相对布局实现居中:
用一张图总结相对定位:
链
如果两个或以上控件通过下图的方式约束在一起,就可以认为是他们是一条链(图为横向的链,纵向同理):
一条链的第一个控件是这条链的链头,当所有控件的高/宽度都为固定值/wrap_content时,可以在链头中通过设置layout_constraintHorizontal_chainStyle
来改变链的样式:
spread:展开元素 (默认);spread_inside:展开元素,但链的两端贴近 parent;packed:链的元素将被打包在一起。
当所有控件的高/宽度都为0dp时,可以在每个控件中通过设置layout_constraintHorizontal_weight(constraintVertical为纵向)
来改变链的权重。
界面显示:
引入封装好的布局
在布局文件中加上一句<include layout="@layout/title"/>
;隐藏系统自带的标题栏:ActionBar actionBar = getSupportActionBar();if (actionBar != null) actionBar.hide();
自定义控件
不光布局会被重复使用,某些控件其功能是固定的,比如返回按钮,都是销毁当前活动。因此也可以对其进行封装复用,创建一个自定义类TitleLayout.java继承LinearLayout,并且重写里面的构造方法:
public class TitleLayout extends LinearLayout {public TitleLayout(Context context, Attributes attrs){super(context, (AttributeSet) attrs);LayoutInflater.from(context).inflate(R.layout.title, this);}}
此时,在布局中引入TitleLayout控件就会调用这个构造函数,因此使用LayoutInflater来实现动态加载,from()
方法可以构建出一个LinearLayout对象,然后调用inflate
可以动态加载一个布局文件,里面传入两个参数:
加载布局文件的id
;参数一的父布局。
现在可以在其他xml
文件中(比如second_layout.xml
)添加这个自定义控件:
<com.example.activitytest.TitleLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"></com.example.activitytest.TitleLayout>
com.example.activitytest
是TitleLayout文件所在的完整路径名。如此一来即可把title
布局界面直接搬到second_layout
布局中,那么SecondActivity其显示的布局自然就是title.xml
的样子。
此时我们可以为布局中的控件注册点击事件: