我正在尝试创建自定义 xml 属性,这些属性可以被更多类接受。我发现(除其他外)这个有用的答案:Defining custom attrs
但是我如何以编程方式访问属性?Eclipse 似乎没有找到定义的名称:
这是我的 attrs.xml,它是我正在尝试使用的 ViewPadding:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="ViewPadding" format="integer"/>
<declare-styleable name="IntegerPicker">
<attr name="maxNrOfDigits" format="integer"/>
<attr name="NumberSize" format="integer"/>
<attr name="ViewColor" format="color"/>
<attr name="TextAlignRight" format="boolean"/>
</declare-styleable>
<declare-styleable name="DoublePicker">
<attr name="CompoundViewColor" format="color"/>
<attr name="CompoundViewPadding" format="integer"/>
</declare-styleable>
</resources>
这是我用来访问其他属性的代码:
public IntegerPicker(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.integer_picker, this, true);
etInteger = (EditText) findViewById(R.id.etInteger);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IntegerPicker);
final int N = a.getIndexCount();
for (int i = 0; i < N; ++i)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.IntegerPicker_ViewColor:
int ViewColor = a.getColor(attr, 0);
this.setBackgroundColor(ViewColor);
break;
case R.styleable.IntegerPicker_maxNrOfDigits:
int ems = a.getInteger(attr, 8);
etInteger.setEms(ems);
break;
case R.styleable.:
int padding = a.getInteger(attr, 10);
etInteger.setPadding(padding, padding, padding, padding);
break;
case R.styleable.IntegerPicker_NumberSize:
int NumberSize = a.getInteger(attr, 10);
etInteger.setTextSize(NumberSize);
break;
case R.styleable.IntegerPicker_TextAlignRight:
boolean textAlignRight = a.getBoolean(attr,false);
if(textAlignRight){
etInteger.setGravity(0x05);
}else{
etInteger.setGravity(0x03);
}
}
}
a.recycle();
}
我该如何处理:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.integer_picker, this, true);
和
case R.styleable.IntegerPicker_ViewColor:
int ViewColor = a.getColor(attr, 0);
this.setBackgroundColor(ViewColor);
请帮忙,我真的很茫然:S