标签:
获取自定义属性大家都很熟悉了,就不多说了(定义declare-styleable,context.obtainStyledAttributes巴拉巴拉...)
下面我们说一下怎么获取非自定义的属性,比如android:entries,android:gravity等等,上面的方式照着套是行不通了,因为你无法取得android.R.styleable里面的东西,下面是我试出来的几种方法:
1. 遍历AttributeSet,基本可以把所有xml里的属性取出来,但是有一点,取出来的值要能使用,需要经过一定处理,具体代码如下:
1 //find entries 2 int count = attrs.getAttributeCount(); 3 for (int i = 0; i < count; i++) { 4 if ("entries".equals(attrs.getAttributeName(i))) { 5 int resid = attrs.getAttributeResourceValue(i, 0); 6 if (resid > 0) { 7 String[] entries = context.getResources().getStringArray(resid); 8 if (entries != null) { 9 //get gravity 10 int g = -1; 11 for (int j = 0; j < count; j++) { 12 if ("gravity".equals(attrs.getAttributeName(j))) { 13 g = j; 14 break; 15 } 16 } 17 final int gravity = attrs.getAttributeIntValue(g, Gravity.CENTER); 18 //create adapter 19 ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, R.layout.item_spinner_top, R.id.text, entries) { 20 @Override 21 public View getView(int position, View convertView, ViewGroup parent) { 22 View view = super.getView(position, convertView, parent); 23 TextView text = ButterKnife.findById(view, R.id.text); 24 text.setTextColor(textColor); 25 text.setTextSize(DimenUtils.px2dp(textSize)); 26 text.setGravity(gravity); 27 return view; 28 } 29 30 @Override 31 public View getDropDownView(int position, View convertView, ViewGroup parent) { 32 View dropDownView = super.getDropDownView(position, convertView, parent); 33 TextView text = ButterKnife.findById(dropDownView, R.id.text); 34 text.setTextColor(dropdownTextColor); 35 text.setTextSize(DimenUtils.px2dp(textSize)); 36 text.setGravity(gravity); 37 return dropDownView; 38 } 39 }; 40 adapter.setDropDownViewResource(R.layout.item_spinner_dropdown); 41 setAdapter(adapter); 42 } 43 } 44 break; 45 } 46 }
2. 还是使用context.obtainStyledAttributes,数组可以从android.R.attr里面取(不需要styleable了),处理起来跟以前差不多,很好用,推荐
private static final int[] RX_SPINNER_OVERRIDE_ATTRS = { android.R.attr.entries, android.R.attr.gravity }; a = context.obtainStyledAttributes(attrs,RX_SPINNER_OVERRIDE_ATTRS); if (a.hasValue(0)){ CharSequence[] entries = a.getTextArray(0); final int gravity = a.getInt(1,Gravity.CENTER); //create adapter ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, R.layout.item_spinner_top, R.id.text, entries) { @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView text = ButterKnife.findById(view, R.id.text); text.setTextColor(textColor); text.setTextSize(DimenUtils.px2dp(textSize)); text.setGravity(gravity); return view; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View dropDownView = super.getDropDownView(position, convertView, parent); TextView text = ButterKnife.findById(dropDownView, R.id.text); text.setTextColor(dropdownTextColor); text.setTextSize(DimenUtils.px2dp(textSize)); text.setGravity(gravity); return dropDownView; } }; adapter.setDropDownViewResource(R.layout.item_spinner_dropdown); setAdapter(adapter); } a.recycle();
在使用中无意发现另外一个问题,tint相关属性即使设置了,在5.0以下也是拿不到的,究其原因应该是这个是api level 21才引入的,低于这个的机器上被忽略了,那么要同时兼容高低版本的话,可以使用v7包里的tint属性,android.support.v7.appcompact.R.attr.backgroundTint等,使用的时候就不要用android:backgroundTint了,改用app:backgroundTint即可。
标签:
原文地址:http://www.cnblogs.com/DarkMaster/p/4811057.html