1.在values下新建xml文件为atts.xml,内容如下,declare-styleable声明自定义控件,name="Topbar" 定义控件的名字,<attr/>里面都是自定义的属性 name="属性名字" formar="属性的类型"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="title" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="leftText" format="string"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
</declare-styleable>
</resources>
2.定义一个类Topbar继承自RelativeLayout
package xss.customview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Topbar extends RelativeLayout {
private Button leftButton, rightButton;
private TextView tvTitle;
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
private String title;
private int titleTextColor;
private float tilteTextSize;
// 定义接口回掉方法
private topbarClickListener listener;
//自定义接口
public interface topbarClickListener {
public void leftClick();
public void rightClick();
}
//接口的实现方法
public void setOnTopbarClickListener(topbarClickListener listener) {
this.listener = listener;
}
private LayoutParams leftParams, rightParams, titleParams;
// 利用构造函数从定义的xml文件中得到属性值
@SuppressLint("NewApi")
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
// 获取属性值
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.Topbar);
leftText = ta.getString(R.styleable.Topbar_leftText);
leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
rightText = ta.getString(R.styleable.Topbar_rightText);
rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
title = ta.getString(R.styleable.Topbar_title);
tilteTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
ta.recycle();// 及时回收,避免浪费资源,避免有些缓存造成的错误
// 处理定义的控件,为控件设置属性
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
leftButton.setBackground(leftBackground);
leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);
rightButton.setBackground(rightBackground);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
tvTitle.setText(title);
tvTitle.setTextSize(tilteTextSize);
tvTitle.setTextColor(titleTextColor);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xFFF59563);// 设置背景颜色
// 把自定义的控件添加到布局当中
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton, leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightButton, rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle, titleParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.leftClick();//调用自定义接口里面的方法
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.rightClick();//调用自定义接口里面的方法
}
});
}
//设置按钮是否可见
public void setLeftButtonIsVisable(boolean flag){
if(flag){
leftButton.setVisibility(View.VISIBLE);
}else{
leftButton.setVisibility(View.GONE);
}
}
}
3.在布局文件中引用自定义的控件布局
其中app为命名空间,加上xmlns:app="http://schemas.android.com/apk/res/xss.customview"才能引用自定义的属性
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/xss.customview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<xss.customview.Topbar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
app:leftText="Back"
app:leftTextColor="#000000"
app:leftBackground="@drawable/button_bg"
app:rightText="Click"
app:rightTextColor="#FFFFFF"
app:rightBackground="@drawable/button_bg"
app:title="自定义标题"
app:titleTextSize="12sp"
app:titleTextColor="#123412" >
</xss.customview.Topbar>
</RelativeLayout>
4.MainActivity里面的实现如下
package xss.customview;
import xss.customview.Topbar.topbarClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Topbar topbar = (Topbar) findViewById(R.id.topbar);
topbar.setOnTopbarClickListener(new topbarClickListener() {
@Override
public void rightClick() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "RIGHT", Toast.LENGTH_SHORT).show();
}
@Override
public void leftClick() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "LEFT", Toast.LENGTH_SHORT).show();
}
});
topbar.setLeftButtonIsVisable(false);//false 为不可见
}
}