首页 > 移动平台 > 详细

Android自定义属性

时间:2014-09-18 19:08:14      阅读:239      评论:0      收藏:0      [点我收藏+]

1   定义属性如下:

    <resources>

    

        <declare-styleable name="social_ivAndTv">

            <attr name="imageResource" format="integer" />

            <attr name="title" format="string" />

        </declare-styleable>

    

    </resources>


2 在自定义组件中引用(加粗字体):

        

public ImageViewAndTextView(Context context, AttributeSet attrs) {

super(context, attrs);

     TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.social_ivAndTv);

     resourceId = array.getResourceId(R.styleable.social_ivAndTv_imageResource, 0);

     content = array.getString(R.styleable.social_ivAndTv_title);

     initView(context);

}


3 在布局文件中引用:

        3.1在顶部添加命名空间:

                xmlns:app="http://schemas.android.com/apk/res/包名"

        3.2 在自定义组建中添加自定义属性:               

                app:imageResource="@drawable/socialweibo_icon_neutral_normal"

                app:title="正面" 


4 完成工作。



Android自定义属性

原文:http://zhongkun.blog.51cto.com/1057583/1554837

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!