0
我想從我的自定義視圖類的xml佈局文件中獲取屬性,似乎有兩種方法可以做到這一點....是其中一種方法更好的實踐或類似的東西?爲自定義視圖獲取屬性的正確方法?
這第一種是使用一個類型數組訪問所有屬性
public VisualNode(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//getting all the attributes that might be set in an xml file
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.VisualNode, 0, 0);
String text = a.getString(R.styleable.VisualNode_device_name);
deviceName = new TextView(context);
deviceName.setText(text);
與這一個直接訪問
deviceName = new TextView(context);
deviceName.setText(R.styleable.VisualNode_device_name);