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导致控件不能预览。


转----http://www.cnblogs.com/vivid-stanley/archive/2012/05/25/2518500.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值