700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android入门(八) | 常用的界面布局 及 自定义控件

Android入门(八) | 常用的界面布局 及 自定义控件

时间:2024-04-23 11:03:27

相关推荐

Android入门(八) | 常用的界面布局 及 自定义控件

文章目录

LinearLayout :线性布局android:layout_gravity :控件的对齐方式android:layout_weight:权重RelativeLayout :相对布局相对于父布局进行定位相对于控件进行定位边缘对齐FrameLayout :帧布局Percent :百分比布局ConstraintLayout :约束布局自定义控件封装复用的页面引入封装好的布局自定义控件

LinearLayout :线性布局

线性布局有水平、垂直两种排列方式:

android:orientation="vertical":垂直方向排列,此时高度不可被指定为match_parentandroid:orientation="horizontal":水平方向排列,此时不能将宽度指定为match_parent

android:layout_gravity :控件的对齐方式

如果布局方式选择horizontal,之后设置button1topbutton2center_verticalbutton3bottom。那么呈现效果如下:

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支持该功能,因此提供了PercentFrameLayoutPercentRelativeLayout分别解帧布局和相对布局的功能局限性。

具体来说,即可以不再使用wrap_contentmatch_parent等方式来指定控件大小,而是直接指定控件在布局中所占的百分比。

使用时,由于 Android 将百分比布局定义在了support库中,因此只需在app/build.gradle文件中添加下面依赖,需要注意的是 support 库在 Androidx 1.0.0 及更高版本中被AndroidX库完全取代了……因此添加依赖时需如此实现:

只用完整路径androidx.percentlayout.widget.PercentFrameLayout作为标签名,因为百分比布局不像其他三个内置在系统中。必须定义一个命名空间 app 才能使用百分比布局的自定义属性。使用layout_widthPercentlayout_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_backapp:layout_constraintLeft_toLeftOf="parent":意为将title_back左边缘约束到父布局左边缘title_editapp: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.activitytestTitleLayout文件所在的完整路径名。如此一来即可把title布局界面直接搬到second_layout布局中,那么SecondActivity其显示的布局自然就是title.xml的样子。

此时我们可以为布局中的控件注册点击事件:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。