方式:繼承一個合適的ViewGroup悲酷,再添加指定功能的控件粟关,需要拓展一些可以配置的屬性疮胖,以達到更好的重用功能。
以一個自定義標題欄為例闷板。
具體功能:標題欄分為三個部分澎灸,左按鈕、右按鈕蛔垢、標題击孩,按鈕可以設(shè)置文字迫悠、文字顏色鹏漆、背景,并且可以隱藏创泄;標題可以設(shè)置文字艺玲、文字顏色、文字大小鞠抑、背景饭聚。
步驟:
1.定義標題欄的屬性,如標題顏色搁拙、大小等等秒梳,需要在res/values目錄下創(chuàng)建一個attrs.xml文件,并通過如下代碼定義相應(yīng)的屬性箕速。
<resources>
<declare-styleable name="topBar">
<attr name="titleText" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="titleBackground" 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>
2.創(chuàng)建標題欄類TopBar酪碘,并繼承一個ViewGroup,這里選擇RelativeLayout盐茎。我們需要在其構(gòu)造方法中獲取自定義屬性集兴垦,即attrs.xml里面定義的屬性,這里用到了TypedArray字柠,通過它的getString()探越、getColor()等方法獲取屬性值。
public class TopBar extends RelativeLayout{
private String mLeftText;
private int mLeftTextColor;
private Drawable mLeftBackground;
private String mRightText;
private int mRightTextColor;
private Drawable mRightBackground;
private String mTitleText;
private int mTitleTextColor;
private float mTitleTextSize;
private Drawable mTitleBackground;
private Button mLeftButton;
private Button mRightButton;
private TextView mTitleView;
private LayoutParams mLeftParams;
private LayoutParams mRightParams;
private LayoutParams mTitleParams;
private TopBarClickListener mListener;
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
//存儲自定義的屬性集
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.topBar);
//取出TypedArray中的值
mLeftText = ta.getString(R.styleable.topBar_leftText);
mLeftTextColor = ta.getColor(R.styleable.topBar_leftTextColor, 0);
mLeftBackground = ta.getDrawable(R.styleable.topBar_leftBackground);
mRightText = ta.getString(R.styleable.topBar_rightText);
mRightTextColor = ta.getColor(R.styleable.topBar_rightTextColor, 0);
mRightBackground = ta.getDrawable(R.styleable.topBar_rightBackground);
mTitleText = ta.getString(R.styleable.topBar_titleText);
mTitleTextColor = ta.getColor(R.styleable.topBar_titleTextColor, 0);
mTitleTextSize = ta.getDimension(R.styleable.topBar_titleTextSize, 10);
mTitleBackground = ta.getDrawable(R.styleable.topBar_titleBackground);
//回收資源
ta.recycle();
//為控件的屬性賦值
init(context);
}
private void init(Context context){
mLeftButton = new Button(context);
mRightButton = new Button(context);
mTitleView = new TextView(context);
mLeftButton.setText(mLeftText);
mLeftButton.setTextColor(mLeftTextColor);
mLeftButton.setBackground(mLeftBackground);
mRightButton.setText(mRightText);
mRightButton.setTextColor(mRightTextColor);
mRightButton.setBackground(mRightBackground);
mTitleView.setText(mTitleText);
mTitleView.setTextColor(mTitleTextColor);
mTitleView.setTextSize(mTitleTextSize);
mTitleView.setBackground(mTitleBackground);
mTitleView.setGravity(Gravity.CENTER);
mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(mLeftButton, mLeftParams);
mRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(mRightButton, mRightParams);
mTitleParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(mTitleView, mTitleParams);
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.leftClick();
}
});
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.rightClick();
}
});
}
/**
* 注冊接口回調(diào)
* @param listener
*/
public void setOnTopBarClickListener(TopBarClickListener listener){
mListener = listener;
}
/**
* 左右按鈕點擊事件的接口窑业,發(fā)生點擊事件時回調(diào)方法
*/
public interface TopBarClickListener{
void leftClick();
void rightClick();
}
/**
* 控制按鈕是否顯示
* @param id 0:左钦幔,1:右
* @param flag true:顯示 false:隱藏
*/
public void setButtonDisplay(int id, boolean flag){
if (flag){
if (id == 0){
mLeftButton.setVisibility(View.VISIBLE);
}else{
mRightButton.setVisibility(View.VISIBLE);
}
}else{
if (id == 0){
mLeftButton.setVisibility(View.GONE);
}else{
mRightButton.setVisibility(View.GONE);
}
}
}
}
3.引用自定義標題欄:需要指定第三方也就是我們自定義的命名空間,我定義的是 xmlns:topBar="http://schemas.android.com/apk/res-auto" 常柄,當然节槐,這里的topBar你可以替換成任意你想定義的名字搀庶,比如one、two铜异、three都應(yīng)該沒什么問題哥倔。
<priv.ky2.viewdemo.widget.TopBar
xmlns:topBar="http://schemas.android.com/apk/res-auto"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/light_black"
topBar:leftText="Left"
topBar:leftTextColor="@color/white"
topBar:rightText="Right"
topBar:rightTextColor="@color/white"
topBar:titleText="Title"
topBar:titleTextSize="10sp"
topBar:titleTextColor="@color/white"/>
注:左右按鈕點擊接口回調(diào)
mTopBar = (TopBar) findViewById(R.id.topBar);
mTopBar.setOnTopBarClickListener(new TopBar.TopBarClickListener() {
@Override
public void leftClick() {
}
@Override
public void rightClick() {
}
});