创建复合控件可以很好的创建出具有重用功能的控件集合,这种方式通常是需要继承一个合适的ViewGroup,再添加指定的控件,形成新的控件。
下面的例子基于《Android群英传》实现TopBar:
定义属性:
<resources>
<declare-styleable name="TopBar">
<!-- 标题内容 -->
<attr name="title_text" format="string"/>
<!-- 标题字体大小 -->
<attr name="title_text_size" format="dimension"/>
<!-- 标题颜色 -->
<attr name="title_text_color" format="color"/>
<!-- 左边按钮的文字 -->
<attr name="left_text" format="string"/>
<!-- 左边按钮文字颜色 -->
<attr name="left_text_color" format="color"/>
<!-- 左边按钮的背景 -->
<attr name="left_bg" format="reference|color"/>
<!-- 右边按钮文字 -->
<attr name="right_text" format="string"/>
<!-- 右边按钮文字颜色 -->
<attr name="right_text_color" format="color"/>
<!-- 右边按钮背景 -->
<attr name="right_bg" format="reference|color"/>
</declare-styleable>
</resources>
自定义VIew:
public class TopBar extends RelativeLayout {
/**
* 左边按钮的文字颜色
*/
private int mLeftTextColor;
/**
* 左边按钮的背景
*/
private Drawable mLeftBackground;
/**
* 左边按钮的文字
*/
private String mLeftText;
/**
* 右边按钮的文字颜色
*/
private int mRightTextColor;
/**
* 右边按钮的背景
*/
private Drawable mRightBackground;
/**
* 右边按钮的文字
*/
private String mRightText;
/**
* 标题文字的颜色
*/
private int mTitleTextColor;
/**
* 标题文字的大小
*/
private float mTitleTextSize;
/**
* 标题文字
*/
private String mTitleText;
/**
* 左边按钮
*/
private Button mLeftButton;
/**
* 右边按钮
*/
private Button mRightButton;
/**
* 标题
*/
private TextView mTitleView;
private LayoutParams mLeftParams;
private LayoutParams mRightParams;
private LayoutParams mTitleParams;
private OnTopBarClickListener mListener;
//添加点击接口
public void setOnTopBarClickListener(OnTopBarClickListener listener) {
mListener = listener;
}
//定义接口
public interface OnTopBarClickListener {
// 左按钮点击事件
void leftClick();
// 右按钮点击事件
void rightClick();
}
public TopBar(Context context) {
super(context);
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
// 从 TypedArray 中取出各属性对应的值
// 取出的值是我们在 layout_topbar.xml 中给各属性赋的值
mLeftTextColor = typedArray.getColor(R.styleable.TopBar_left_text_color, 0);
mLeftBackground = typedArray.getDrawable(R.styleable.TopBar_left_bg);
mLeftText = typedArray.getString(R.styleable.TopBar_left_text);
mRightTextColor = typedArray.getColor(R.styleable.TopBar_right_text_color, 0);
mRightBackground = typedArray.getDrawable(R.styleable.TopBar_right_bg);
mRightText = typedArray.getString(R.styleable.TopBar_right_text);
mTitleTextColor = typedArray.getColor(R.styleable.TopBar_title_text_color, 0);
mTitleTextSize = typedArray.getDimension(R.styleable.TopBar_title_text_size, 10);
mTitleText = typedArray.getString(R.styleable.TopBar_title_text);
// 获取完 TypedArray 的值后,
// 一般要调用 recycle 方法来避免重新创建的时候出错
typedArray.recycle();
//创建控件
mLeftButton = new Button(context);
mRightButton = new Button(context);
mTitleView = new TextView(context);
// 为创建的组件元素赋值
mLeftButton.setBackgroundDrawable(mLeftBackground);
mLeftButton.setText(mLeftText);
mLeftButton.setTextColor(mLeftTextColor);
mRightButton.setBackgroundDrawable(mRightBackground);
mRightButton.setText(mRightText);
mRightButton.setTextColor(mRightTextColor);
mTitleView.setTextColor(mTitleTextColor);
mTitleView.setTextSize(mTitleTextSize);
mTitleView.setText(mTitleText);
// 标题文字在 TextView 中心
mTitleView.setGravity(Gravity.CENTER);
// 左边按钮的布局参数
mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
// 左边按钮在父布局的左边
mLeftParams.addRule(ALIGN_PARENT_LEFT, TRUE);
// 添加到父布局
addView(mLeftButton, mLeftParams);
// 右边按钮的布局参数
mRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
// 右边按钮在父布局的右边
mRightParams.addRule(ALIGN_PARENT_RIGHT, TRUE);
// 添加到父布局
addView(mRightButton, mRightParams);
// 标题的布局参数
mTitleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
// 标题在父布局的中心
mTitleParams.addRule(CENTER_IN_PARENT, TRUE);
// 添加到父布局
addView(mTitleView, mTitleParams);
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.leftClick();
}
}
});
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.rightClick();
}
}
});
}
public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
public class Main2Activity extends AppCompatActivity {
private TopBar mTopBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTopBar = (TopBar) findViewById(R.id.topBar);
mTopBar.setOnTopBarClickListener(new TopBar.OnTopBarClickListener() {
@Override
public void leftClick() {
Toast.makeText(Main2Activity.this, "返回", Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(Main2Activity.this, "更多", Toast.LENGTH_SHORT).show();
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hongbo.customview_demo.Main2Activity">
<com.hongbo.customview_demo.TopBar
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="40dp"
app:left_bg="@mipmap/blue_button"
app:left_text="返回"
app:left_text_color="#FFFFFF"
app:right_bg="@mipmap/blue_button"
app:right_text="更多"
app:right_text_color="#FFFFFF"
app:title_text="自定义标题"
app:title_text_color="#123412"
app:title_text_size="10sp">
</com.hongbo.customview_demo.TopBar>
</RelativeLayout>
如图: