温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android自定义属性

发布时间:2020-07-16 21:18:02 来源:网络 阅读:557 作者:510202 栏目:移动开发

1   定义属性如下:

    <resources>

    

        <declare-styleable name="social_ivAndTv">

            <attr name="p_w_picpathResource" 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_p_w_picpathResource, 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:p_w_picpathResource="@drawable/socialweibo_icon_neutral_normal"

                app:title="正面" 


4 完成工作。



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI