一.实现思路
先创建一个继承RelativeLayout的类,创建与它相对应的布局,布局随意。给它一个方法获取控件id,然后设置基本属性内容,最后设置监听。
创建attrs设置标题栏的基本属性,然后在activity_main中设置这个自定义控件。
二.实现的代码步骤
titleset.xml布局,设置一个自定义标题栏,控件自己可以随意设计
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="56dp"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<ImageView
android:id="@+id/imageview"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</RelativeLayout>
在values里创建attrs.xml,通过这个布局调用在主布局中设置的属性
<declare-styleable name="TitleSet">
<!--设置标题的颜色-->
<attr name="MYZT" format="color" />
<!--设置标题内容-->
<attr name="MYR" format="string" />
<!--设置标题大小-->
<attr name="mysize" format="integer" />
<!--设置图片颜色-->
<attr name="photo" format="color"/>
</declare-styleable>
创建TitleSet类继承RelativeLayout
public class TitleSet extends RelativeLayout {
TextView textView;//标题
ImageView imageView;//图片
public BarClickListener listener;//监听
public void setListener(BarClickListener listener) {
this.listener = listener;
}
/**
* 一种参数
* @param context
*/
public TitleSet(Context context) {
super(context);
initview(null);
}
/**
* 两种参数
* @param context
* @param attrs
*/
public TitleSet(Context context, AttributeSet attrs) {
super(context, attrs);
initview(attrs);
}
private void initview(AttributeSet attrs) {
//获取布局
View v = LayoutInflater.from(getContext()).inflate(R.layout.titleset, this);
textView = (TextView) v.findViewById(R.id.textview);
imageView = (ImageView) v.findViewById(R.id.imageview);
//标题的监听
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listener!=null){
listener.titleListener(v);
}
}
});
//图片的监听
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener!=null){
listener.imageListener(v);
}
}
});
//attrs判空
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TitleSet);
//标题栏
String str = a.getString(R.styleable.TitleSet_MYR);
//设置字体颜色,默认黑色
int co= a.getColor(R.styleable.TitleSet_MYZT, Color.BLACK);
textView.setTextColor(co);
//设置字体大小
int size= a.getInteger(R.styleable.TitleSet_mysize,20);
textView.setTextSize(size);
//设置图片的颜色,默认红色
int ph= a.getColor(R.styleable.TitleSet_photo,Color.RED);
imageView.setBackgroundColor(ph);
///如果这是一个空的
if (TextUtils.isEmpty(str)) {
//就默认项目名称
textView.setText(getResources().getString(R.string.app_name));
} else {
textView.setText(str);
}
a.recycle();//一定记得销毁
}
}
//设置监听的方法
public interface BarClickListener{
public void imageListener(View v);
public void titleListener(View v);
}
}
activity_main.xml布局
<com.example.rikaojineng8_04.TitleSet
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MYR="标题栏"
app:mysize="25"
app:photo="#f3f3"
app:MYZT="#000" />
MainActivity类
public class MainActivity extends AppCompatActivity implements TitleSet.BarClickListener{
TitleSet titleSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleSet= (TitleSet) findViewById(R.id.title);
titleSet.setListener(this);
}
@Override
public void imageListener(View v) {
Toast.makeText(this,"图片",Toast.LENGTH_LONG).show();
}
@Override
public void titleListener(View v) {
Toast.makeText(this,"标题",Toast.LENGTH_LONG).show();
}
}