Android开发学习教程(17)- Android自定义控件

0 1,128

—— 你不能把这个世界让给你所鄙视的人

前几篇我们讲了基本控件和常用布局的基本用法,这里来从整体上看看他们的继承关系。

Android开发学习教程(17)- Android自定义控件

可以看到,我们所用的控件最终都是继承自View,View是Android中最基本的用户界面组件,它占据屏幕上的一块矩形区域,并且负责绘图和事件处理。我们所用的常用布局是继承自ViewGroup,ViewGroup是一种特殊的View,它包含了多个拥有LayoutParams属性的View。

引入布局

如果你经常使用APP你会发现,很多APP的界面顶部都会有一个标题栏,标题栏左右是一个返回图标,中间是当前页面的名字。对于这种多个页面都有的样式,如果在每个布局中都编写一遍同样的标题栏样式,明显就会导致代码的大量重复并且不好维护,比如以后改变需求,需要在最右边增加一个分享图标,那岂不是每个都要去修改一遍。这个时候我们可以使用引入布局的方式来解决这个问题,新建一个布局 layout_mytitle.xml,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <ImageView
            android:id="@+id/img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:src="@drawable/back_black1" />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="标题"
            android:textColor="#222222"
            android:textSize="16sp" />
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

然后在每个需要用到此标题栏的布局里引入就可以了,如下:

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <include layout="@layout/layout_mytitle" />
</androidx.constraintlayout.widget.ConstraintLayout>

运行看看效果:

Android开发学习教程(17)- Android自定义控件

创建自定义控件

引入布局的技巧确实解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,我们还需要在每个Activity中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回图标,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即退出当前活动。而如果在每一个活动中都需要重新注册一遍返回按钮的点击事件,无疑又是增加了很多重复代码,这种情况最好是使用自定义控件的方式来解决。

新建 MyTitleLayout 继承自 FramLayout,让它成为我们自定义的标题栏控件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MyTitleLayout extends FrameLayout {
    public MyTitleLayout(Context context, AttributeSet attr) {
        super(context, attr);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_mytitle, this);
        view.findViewById(R.id.img_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击了返回图标", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

首先我们重写了 FrameLayou中的带有两个参数的构造函数,这样我们在布局中引入 MyTitleLayout控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借助 LayoutInflater 来实现了。通过 LayoutInflater 的 from()方法可以构建出一个LayoutInflater 对象,然后调用 inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的 id,这里我们传入 R.layout.layout_mytitle,第二个参数是给加载好的布局添加一个父布局,这里我们想要指定为 MyTitleLayout,于是直接传入 this。

现在自定义控件已经创建好了,然后我们需要在布局文件中添加这个自定义控件,修改activity_main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.example.myapplication1.MyTitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候我们需要指明控件的完整类名,包名在这里是不可以省略的。

重新运行程序,你会发现此时效果和使用引入布局方式的效果是一样的。

Android开发学习教程(17)- Android自定义控件

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章资源,如无特殊说明或标注,均为本站网友和创作者贡献分享。如若本站内容侵犯了原著者的合法权益,可联系网站客服QQ2743319061删除。

云炬星球 安卓教程 Android开发学习教程(17)- Android自定义控件 https://src.yunjunet.cn/876774.html

常见问题
  • 放心亲,我们不会为了几十块钱的东西坏了名声!
查看详情
  • 方法一:点击“立即下载.”按钮,付款后在下载弹窗的虚线框的隐藏信息里获取 方法二:在正文底部使用VIP查看隐藏的解压密码 方法三:联系【云炬网络】公众号客服获取
查看详情
  • 付款后会出现“立即下载”按钮(点击即可下载),如果下载失败也可以联系客服发订单截图补发。
查看详情
  • 登录购买会多端同步购买记录,永久可以查看反复下载;非登录购买仅将购买记录保存到本地浏览器中,浏览器cookie清除后无法再次下载。先右上角点登录,然后点击微信图标可以快速授权注册登录^_^
查看详情
  • 可以试看。点击”查看演示“或“试看预览”按钮可以试读从资料目录中节选的部分内容,也可以自己指定想试看的内容。
查看详情
  • 原因一:本站所有资源已开启有效性检测(服务器24h全自动监测),当监测到下载链接无法访问时会提示“该资源已失效,请勿购买”,遇到这种情况可以联系客服修复失效的下载链接,或直接联系客服在淘宝下单购买即可。(检测原理:购买前服务器程序会预访问下载链接,响应值为200说明资源有效允许购买,响应值为404或502等报错说明资源失效禁止购买)。原因二:上传者未启用“下载”选项。
查看详情
官方客服团队

为您解决烦忧 - 24小时在线 专业服务