不显示文字
//....
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 float titleTextSize;
private int titleTextColor;
private String title;
private LayoutParams leftpParams,rightpParams,titlepParams;
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
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);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 0);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
title = ta.getString(R.styleable.TopBar_title);
ta.recycle();// 避免浪费资源,缓存引起的错误
// 1.获取自定义属性
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
// 2.创建控件
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);
leftButton.setText(leftText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
rightButton.setText(rightText);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setText(title);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xfff59563);
leftpParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftpParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton, leftpParams);
rightpParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rightpParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton, rightpParams);
titlepParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
titlepParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvTitle, titlepParams);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.example.topbar.TopBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.example.topbar.TopBar
android:layout_width="wrap_content"
android:layout_height="40dp"
app:leftText="left"
app:rightText="right"
app:title="自定义标题" >
</com.example.topbar.TopBar>
</RelativeLayout>
atts代码:
<?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="leftBackGround" format="reference|color" />
<attr name="leftTextColor" format="color" />
<attr name="rightText" format="string" />
<attr name="rightBackGround" format="reference|color" />
<attr name="rightTextColor" format="color" />
</declare-styleable>
</resources>