public class TopBar extends RelativeLayout {
private Button leftButton;//左边按钮
private Button rightButton;//右边按钮
private TextView tvTitle; //中间标题
//声明一些控件的属性
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
private int titleTextColor;
private String title;
private float titleTextSize;
//为三个布局形式
private LayoutParams leftParams,rightParams,titleParams;
@SuppressLint("NewApi") public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("info", "调用了自定义的属性");
//拿到自己定义的属性
TypedArray ta=context.obtainStyledAttributes(R.styleable.TopBar);
leftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor, 0);
leftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
leftText=ta.getString(R.styleable.TopBar_leftText);
rightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor, 0);
rightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
rightText=ta.getString(R.styleable.TopBar_rightText);
titleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor, 0);
title=ta.getString(R.styleable.TopBar_title);
titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize, 15);
//回收掉 1、有可能浪费资源。2、避免由于一些直的缓存发生的错误
ta.recycle();
//创建控件
leftButton=new Button(context);
rightButton=new Button(context);
tvTitle=new TextView(context);
//为三个空间设置属性
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);//注意最小的sdk为16
leftButton.setText(leftText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);//注意最小的sdk为16
rightButton.setText(rightText);
tvTitle.setText(title);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);//自己添加的属性使其居中
//设置整体布局的背景颜色
setBackgroundColor(0x6699ff);
//为左部局参数
//第一个属性为宽,第二个为高
leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
//添加控件
addView(leftButton,leftParams);
//为右边局参数
rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
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);
}
}
<?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="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
</declare-styleable>
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res/com.fdd.topbar">
<com.fdd.topbar.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:leftBackground="#000000"
app:leftText="左部按钮"
app:leftTextColor="#ff0000"
app:rightBackground="#00000f"
app:rightText="右部按钮"
app:rightTextColor="#ff00ff"
app:title="标题"
app:titleTextColor="#00ff00"
app:titleTextSize="25sp" />
</RelativeLayout>