前端之家收集整理的这篇文章主要介绍了
自定义控件从xml获取属性值的优雅写法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public@H_403_2@ LinearLayoutManager@H_403_2@(Context context,AttributeSet attrs,int@H_403_2@ defStyleAttr,int@H_403_2@ defStyleRes) {
Properties properties = getProperties(context,attrs,defStyleAttr,defStyleRes);
setOrientation(properties.orientation);
....
}
public@H_403_2@ static@H_403_2@ Properties getProperties@H_403_2@(Context context,int@H_403_2@ defStyleRes) {
Properties properties = new@H_403_2@ 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@H_403_2@);
properties.reverseLayout = a.getBoolean(R.styleable.RecyclerView_reverseLayout,false@H_403_2@);
properties.stackFromEnd = a.getBoolean(R.styleable.RecyclerView_stackFromEnd,false@H_403_2@);
a.recycle();
return@H_403_2@ properties;
}
public@H_403_2@ static@H_403_2@ class@H_403_2@ Properties@H_403_2@ {@H_403_2@
/** @attr@H_403_2@ ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation */@H_403_2@
public@H_403_2@ int@H_403_2@ orientation;
/** @attr@H_403_2@ ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount */@H_403_2@
public@H_403_2@ int@H_403_2@ spanCount;
/** @attr@H_403_2@ ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout */@H_403_2@
public@H_403_2@ boolean@H_403_2@ reverseLayout;
/** @attr@H_403_2@ ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd */@H_403_2@
public@H_403_2@ boolean@H_403_2@ stackFromEnd;
}