1、首先新建属性文件
<?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="leftTextBackgroud" format="reference|color" />
<attr name="leftTextColor" format="color" />
<attr name="rightText" format="string"/>
<attr name="rightTextBackgroud" format="reference|color" />
<attr name="rightTextColor" format="color" />
</declare-styleable>
</resources>
2、创建自定义布局类
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 titleText;
private float titleTextSize;
private int titleTextColor;
private topBarListener tListener;
public void settListener(topBarListener tListener) {
this.tListener = tListener;
}
public interface topBarListener{
public void clickLeft();
public void clickRight();
}
private LayoutParams leftParams,rightParams,titleParams;
@SuppressLint("NewApi")
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//获取属性值
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.topbar);
leftTextColor = ta.getColor(R.styleable.topbar_leftTextColor, 0);
leftBackGround = ta.getDrawable(R.styleable.topbar_leftTextBackgroud);
leftText = ta.getString(R.styleable.topbar_leftText);
rightTextColor = ta.getColor(R.styleable.topbar_rightTextColor, 0);
rightBackGround = ta.getDrawable(R.styleable.topbar_rightTextBackgroud);
rightText = ta.getString(R.styleable.topbar_rightText);
titleText = ta.getString(R.styleable.topbar_title);
titleTextSize = 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.setText(leftText);
leftButton.setBackground(leftBackGround);
leftButton.setTextColor(leftTextColor);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackGround);
tvTitle.setText(titleText);
tvTitle.setTextSize(titleTextSize);
tvTitle.setTextColor(titleTextColor);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xfff5346);
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 View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tListener.clickLeft();
}
});
rightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tListener.clickRight();
}
});
}
}
3、创建监听事件并使用
public class MainActivity extends Activity {
TopBar top ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
top = (TopBar)findViewById(R.id.myTop);
top.settListener(new topBarListener() {
@Override
public void clickRight() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked the right Button!", 1000).show();
}
@Override
public void clickLeft() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked the left Button!", 1000).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.topbardemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<com.example.topbardemo.TopBar
android:id="@+id/myTop"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftText="left"
custom:leftTextColor="#003300"
custom:leftTextBackgroud="#00BFFF"
custom:title="title"
custom:titleTextColor="#000000"
custom:titleTextSize="14sp"
custom:rightText="right"
custom:rightTextColor="#000000"
custom:rightTextBackgroud="#00BFFF">
</com.example.topbardemo.TopBar>
</RelativeLayout>
1、在使用时需要自定义引用值, xmlns:custom="http://schemas.android.com/apk/res/xxx" xxx是工程包名
2、使用完TypedArray需要释放资源 ta.recycle();
上图: