自定义控件从xml获取属性值的优雅写法

前端之家收集整理的这篇文章主要介绍了自定义控件从xml获取属性值的优雅写法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public LinearLayoutManager(Context context,AttributeSet attrs,int defStyleAttr,int defStyleRes) {
        Properties properties = getProperties(context,attrs,defStyleAttr,defStyleRes);
        setOrientation(properties.orientation);
       ....
    }

       public static Properties getProperties(Context context,int defStyleRes) {
            Properties properties = new Properties();
            TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.RecyclerView,defStyleRes);
            properties.orientation = a.getInt(R.styleable.RecyclerView_android_orientation,VERTICAL);
            properties.spanCount = a.getInt(R.styleable.RecyclerView_spanCount,1);
            properties.reverseLayout = a.getBoolean(R.styleable.RecyclerView_reverseLayout,false);
            properties.stackFromEnd = a.getBoolean(R.styleable.RecyclerView_stackFromEnd,false);
            a.recycle();
            return properties;
        }


           public static class Properties {
            /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation */
            public int orientation;
            /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount */
            public int spanCount;
            /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout */
            public boolean reverseLayout;
            /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd */
            public boolean stackFromEnd;
        }
原文链接:https://www.f2er.com/xml/295016.html

猜你在找的XML相关文章