在我们的android日常开发中有时会重复用到一些布局,这个布局是几个控件组成的,其中的每个控件还可以实现各自的事件。
我们可以将这些布局组合成一个自定义的控件,方便我们使用。
如图:
BACK按钮实现返回功能;
Title Text是显示的标题;
EDIT按钮实现弹出一个toast通知:“You clicked Edit button”。
1、创建你需要组合的布局title
新建custom_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="#112299">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title 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="5dip"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>
2、创建TitleLayout
使用custom_title.xml布局,加载TitleLayout,实现自己特定的功能。
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.activity_title, this);
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();
}
});
}
}
3、自定义控件的用法
如图在布局activity_custom_title.xml中使用方法,com.hjqjl.testdemo.TitleLayout可以当成一个控件来用,如图系统自带的Button、TextView等。
<?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">
<com.hjqjl.testdemo.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
4、将属性设置到TitleLayout中(扩展)
在values中创建atrrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TitleLayout1">
<attr name="text1" format="string" />
</declare-styleable>
</resources>
在创建的控件中添加以下代码
TextView tvCustomTitle = findViewById(R.id.title_text);
// 将属性值设置到控件中
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout1);
CharSequence text = a.getText(R.styleable.TitleLayout1_text1);
if (text != null && !StringUtils.isEmpty(text.toString().trim())) {
tvCustomTitle.setText(text);
}
a.recycle();
使用方法
<com.hjqjl.testdemo.TitleLayout
android:layout_width="match_parent"
app:text1="关于我们"
android:layout_height="wrap_content" />