Android 自定义组合控件View


 

要点:

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,  thistrue);

        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);
        }

    }

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 >

5. 如果你先在eclipse中预览, 千万要注意, 要重启Eclipse,Refresh 资源文件夹。 不然会出现ResourceNotFound导致控件不能预览。

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM