避免使用自定义属性引起的库依赖问题
创始人
2024-12-16 18:30:26
0

在代码编写过程中,尽量避免使用自定义属性来定义功能,而是应该通过编写自己的代码来实现所需的功能。若必须使用自定义属性,可以通过下面的示例中的方法来解决依赖问题:

public class CustomView extends View {

    private int customAttribute = 0;

    public CustomView(Context context) {
        super(context);
        init(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
        customAttribute = a.getInt(R.styleable.CustomView_customAttribute, 0);
        a.recycle();
    }

}

在上面的代码示例中,CustomView 中使用了自定义属性 customAttribute ,但是通过使用 TypedArray 对其进行获取,来避免了对 R 文件依赖的问题。注意要在 attrs 中声明自定义属性的名称和类型,如下所示:


    

相关内容

热门资讯

透视了解!哈糖大菠萝破解器,w... 透视了解!哈糖大菠萝破解器,werplan脚本-其实是有辅助脚本(哔哩哔哩)哈糖大菠萝破解器透视方法...
透视必备!wpk有辅助器吗,w... 透视必备!wpk有辅助器吗,wpk系统是否存在作弊行为-总是一直总是有辅助攻略(哔哩哔哩)1、上手简...
透视揭露!wpk辅助软件,wp... 透视揭露!wpk辅助软件,wpk透视是真的假的-总是是真的有辅助软件(哔哩哔哩)1、每一步都需要思考...
透视教你!wepoker私人局... 透视教你!wepoker私人局怎么玩,wepoker透视版下载-总是存在有辅助技巧(哔哩哔哩)1、完...
透视解谜!哈糖大菠萝辅助器,p... 透视解谜!哈糖大菠萝辅助器,pokemmo辅助器-切实有辅助工具(哔哩哔哩)1.哈糖大菠萝辅助器 选...
透视解迷!aapoker怎么选... 透视解迷!aapoker怎么选牌,aapoker公共底牌-果然是有辅助教程(哔哩哔哩)1、免费脚本咨...
透视普及!we-poker正规... 透视普及!we-poker正规吗,wepoker-h5下载-好像一直总是有辅助技巧(哔哩哔哩)1、首...
透视了解!wepokerplu... 透视了解!wepokerplus外挂,we-poker正规吗-本来真的有辅助神器(哔哩哔哩)1、操作...
透视分享!wepoker脚本下... 透视分享!wepoker脚本下载,wepoker私局代打-果然存在有辅助神器(哔哩哔哩)1、金币登录...
透视推荐!we poker游戏... 透视推荐!we poker游戏下,wepoker作弊辅助-切实有辅助app(哔哩哔哩)1、进入游戏-...