要点:
1.定义Layout 文件 如header.xml
2. 继承类FrameLayout(或者ViewGroup, 或者View)
public HeaderBar(Context context, AttributeSet attrs) {
this(context, attrs, R.style.headerTitleBarStyle);
}
public HeaderBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater. from(context).inflate(R.layout.header, this, true);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HeaderBar);
mLeftButtonBg = a.getDrawable(R.styleable.HeaderBar_leftButtonBackground);
if (mLeftButtonBg == null) {
mLeftButtonBg = context.getResources().getDrawable(R.drawable.back_btn_selector);
}
mRightButtonBg = a.getDrawable(R.styleable.HeaderBar_rightButtonBackground);
if (mRightButtonBg == null) {
mRightButtonBg = context.getResources().getDrawable(R.drawable.refresh);
}
mTitleTextViewButtonBg = a.getDrawable(R.styleable.HeaderBar_titleTextViewBackground);
mTitle = a.getText(R.styleable.HeaderBar_title);
a.recycle();
}
this(context, attrs, R.style.headerTitleBarStyle);
}
public HeaderBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater. from(context).inflate(R.layout.header, this, true);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HeaderBar);
mLeftButtonBg = a.getDrawable(R.styleable.HeaderBar_leftButtonBackground);
if (mLeftButtonBg == null) {
mLeftButtonBg = context.getResources().getDrawable(R.drawable.back_btn_selector);
}
mRightButtonBg = a.getDrawable(R.styleable.HeaderBar_rightButtonBackground);
if (mRightButtonBg == null) {
mRightButtonBg = context.getResources().getDrawable(R.drawable.refresh);
}
mTitleTextViewButtonBg = a.getDrawable(R.styleable.HeaderBar_titleTextViewBackground);
mTitle = a.getText(R.styleable.HeaderBar_title);
a.recycle();
}
3. 重载onFinishInflate来获取子控件的引用。
@Override
protected void onFinishInflate() {
// super.onFinishInflate();
this.mTitleTextView = (TextView) this.findViewById(R.id.tv_header_title);
this.mLeftButton = (Button) this.findViewById(R.id.btn_header_left);
this.mRightButton = (Button) this.findViewById(R.id.btn_header_right);
this.mLeftButton.setBackgroundDrawable( this.mLeftButtonBg);
this.mRightButton.setBackgroundDrawable( this.mRightButtonBg);
if ( this.mTitleTextViewButtonBg != null) {
// titleTextViewButtonBg = context.getResources().getDrawable(R.drawable.refresh);
this.mTitleTextView.setBackgroundDrawable( this.mTitleTextViewButtonBg);
}
if ( this.mTitle != null) {
this.mTitleTextView.setText( this.mTitle);
}
}
protected void onFinishInflate() {
// super.onFinishInflate();
this.mTitleTextView = (TextView) this.findViewById(R.id.tv_header_title);
this.mLeftButton = (Button) this.findViewById(R.id.btn_header_left);
this.mRightButton = (Button) this.findViewById(R.id.btn_header_right);
this.mLeftButton.setBackgroundDrawable( this.mLeftButtonBg);
this.mRightButton.setBackgroundDrawable( this.mRightButtonBg);
if ( this.mTitleTextViewButtonBg != null) {
// titleTextViewButtonBg = context.getResources().getDrawable(R.drawable.refresh);
this.mTitleTextView.setBackgroundDrawable( this.mTitleTextViewButtonBg);
}
if ( this.mTitle != null) {
this.mTitleTextView.setText( this.mTitle);
}
}
4. 如何使用?
在需要使用自定义控件的layout文件,以包名+控件名作为标签名
注意:如果需要用自己的属性,要加上自己的命名空间:xmlns:xl=http://schemas.android.com/apk/res/com.xxx.abc
规则是:http://schemas.android.com/apk/res/ + 包名
<
RelativeLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
xmlns:xl
="http://schemas.android.com/apk/res/com.xxx.abc"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:background ="#f9f9f9"
android:orientation ="vertical" >
< com.xxx.abc.view.HeaderBar
android:id ="@+id/lan_video_title_bar"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
xunlei:title ="@string/lan_video_title"
xl:rightButtonBackground ="@drawable/lan_video_add_btn_selector" >
</ com.xxx.abc.view.HeaderBar >
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:background ="#f9f9f9"
android:orientation ="vertical" >
< com.xxx.abc.view.HeaderBar
android:id ="@+id/lan_video_title_bar"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
xunlei:title ="@string/lan_video_title"
xl:rightButtonBackground ="@drawable/lan_video_add_btn_selector" >
</ com.xxx.abc.view.HeaderBar >
5. 如果你先在eclipse中预览, 千万要注意, 要重启Eclipse,Refresh 资源文件夹。 不然会出现ResourceNotFound导致控件不能预览。