2012년 6월 26일 화요일

AttributeSet 이용 방법(안드로이드 attrs)


public MapView(Context context, AttributeSet attrs) {
    // ...

    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.getDimensionPixelSize(2, ViewGroup.LayoutParams.MATCH_PARENT);
    int layout_height = ta.getDimensionPixelSize(3, ViewGroup.LayoutParams.MATCH_PARENT);
    ta.recycle();
}




public MapView(Context context, AttributeSet attrs) {
    super(context, attrs); // After this, use normal getters

    int id = this.getId();
    Drawable background = this.getBackground();
    ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
}

댓글 없음:

댓글 쓰기