<?xml version="1.0" encoding="utf-8"?>
<!-- 在Values中新建atts.xml文件 -->
<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>
//再写一个类
public class Topbar extends RelativeLayout{
private Button leftButton, rightButton;
private TextView textView;
private int leftTextColor, rightTextColor;
private Drawable leftTextBackground, rightTextBackground;
private String leftText, rightText;
private float titleTextSize;
private int titleTextColor;
private String titles;
private LayoutParams leftP, rightP, titleP;
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
leftTextColor = typedArray.getColor(R.styleable.Topbar_leftTextColor, 0);
rightTextColor = typedArray.getColor(R.styleable.Topbar_rightTextColor, 0);
leftTextBackground = typedArray.getDrawable(R.styleable.Topbar_leftBackground);
rightTextBackground = typedArray.getDrawable(R.styleable.Topbar_rightBackground);
leftText = typedArray.getString(R.styleable.Topbar_leftText);
rightText = typedArray.getString(R.styleable.Topbar_rightText);
titleTextSize = typedArray.getDimension(R.styleable.Topbar_titleTextSize, 0);
titleTextColor = typedArray.getColor(R.styleable.Topbar_titleTextColor, 0);
titles = typedArray.getString(R.styleable.Topbar_title);
typedArray.recycle();
leftButton = new Button(context);
rightButton = new Button(context);
textView = new TextView(context);
leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftTextBackground);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightTextBackground);
textView.setText(titles);
textView.setTextColor(titleTextColor);
textView.setTextSize(titleTextSize);
textView.setGravity(Gravity.CENTER);
setBackgroundColor(0xFFF59563);
leftP = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
leftP.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton, leftP);
rightP = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
rightP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightButton, rightP);
titleP = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
titleP.addRule(RelativeLayout.CENTER_HORIZONTAL, TRUE);
addView(textView, titleP);
}
}
//使用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.qihao.mytopbar.MainActivity">
<com.qihao.mytopbar.Topbar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
my:leftBackground="#ffffff"
my:leftText="Back"
my:leftTextColor="#00FF00"
my:rightBackground="#0000FF"
my:rightText="Setting"
my:rightTextColor="#FFFFFF"
my:title="QiHao"
my:titleTextColor="#FF0000"
my:titleTextSize="10sp">
</com.qihao.mytopbar.Topbar>
</RelativeLayout>