目录
本篇文章主要讲解如何创建并使用自定义控件。
先说一下什么情况需要创建自定义控件,当每个活动都需要同一种布局并且每个活动的控件都有相同的功能时使用,如:所有活动都需要返回按钮,这种情况可以创建自定义控件,解决代码重复问题,而不是每个活动都要写一遍这个按钮。
创建自定义控件
1、引入布局
当我们的程序有很多个活动都需要同样的布局时,简单直接的做法是直接在每个活动的布局中编写同样的布局代码,但是这样导致了代码大量重复,这时我们可以使用引入布局的方式来解决这个问题。
下面通过做一个有返回和编辑样式的标题栏,来体验一下如何引入布局。
新建一个布局title.xml,代码如下:
<?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="wrap_content"
android:background="@color/cardview_dark_background">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="返回"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="文章标题"
android:textColor="#fff"
android:textSize="24sp"/>
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="编辑"
android:textColor="#fff" />
</LinearLayout>
下面在程序中使用这个标题栏,csdn6.xml代码:
一行include语句即可将刚才的布局引入。
<?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">
<include layout="@layout/title"/>
</LinearLayout>
下面将系统自带的标题栏隐藏,java类代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_csdn6);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.hide();
}
}
效果如下:
效果可能有点丑QWQ
2、创建自定义控件
引入布局可以解决代码重复问题,但是如果布局中有些控件要求能响应事件,我们还是需要在每个活动中为这些控件单独编写事件代码,如标题栏的返回按钮,所有活动中功能都是相同的,如果在每个活动中重新编写事件代码,会增加重复代码,这种情况就得需要自定义控件的方式解决。
下面尝试实现。
新建一个名为TitleLayout的java类,此java类充当上面的布局文件,代码如下:
LayoutInflater的from()方法可以构建一个LayoutInflater对象,调用inflate()方法可以动态加载布局文件,有两个参数,第一个是要加载的布局文件的id,第二个是父布局。
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs){
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
}
}
在布局文件中添加自定义控件,引入刚才自定义的控件,代码如下:
<?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">
<com.example.bjtu1.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
此时发现,效果和上面的引入布局操作一样。
下面尝试为标题栏中的按钮注册点击事件,TitleLayout代码如下:
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs){
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
Button titleBack = findViewById(R.id.title_back);
Button titleEdit = findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity)getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "你点击了编辑按钮", Toast.LENGTH_SHORT).show();
}
});
}
}
效果如下:
这样,每当我们在一个布局中引入一个TitleLayout时,返回按钮和编辑按钮的点击事件已经自动实现好,避免代码重复了。
希望本文章对你有帮助,如果你对Android开发感兴趣,请持续关注本专栏,帮助你从入门到项目实战,你将收获:Android基础开发、各种经典功能实现、项目实战、开发自己的APP、将APP上传应用商店、靠广告赚钱等等,持续更新ing......