給定一個 AttributeSet attrs (好比在 LayoutInflater 裏面,還沒有建立 view 以前就已經解析出了 attrs)怎樣獲取特定的屬性 呢:java
int[] attrsArray = new int[] { android.R.attr.id, // 0 android.R.attr.background, // 1 android.R.attr.layout_width, // 2 android.R.attr.layout_height // 3 }; TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray); int id = ta.getResourceId(0 /* index of attribute in attrsArray */, View.NO_ID); Drawable background = ta.getDrawable(1); int layout_width = ta.getLayoutDimension(2, ViewGroup.LayoutParams.MATCH_PARENT); int layout_height = ta.getLayoutDimension(3, ViewGroup.LayoutParams.MATCH_PARENT); ta.recycle();