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