自定义ViewGroup获取子View参数

文中代码引用ApiDemos

参考官方网址[http://developer.android.com/intl/zh-cn/reference/android/view/ViewGroup.html]

其实和正常获取View参数方法一样,只不过获取的地方与方法不一样而已

在这里获取参数

  // ----------------------------------------------------------------------
    // The rest of the implementation is for custom per-child layout parameters.
    // If you do not need these (for example you are writing a layout manager
    // that does fixed positioning of its children), you can drop all of this 
 @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new CustomLayout.LayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

本人写的Demo

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        Log.e(TAG, " --------------------------generateLayoutParams AttributeSet attrs");
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CusLayout_parm);
        boolean is = a.getBoolean(R.styleable.CusLayout_parm_a, false);
        float b = a.getDimension(R.styleable.CusLayout_parm_b,0);
        Log.e(TAG, " is = " + is);
        Log.e(TAG, " b = " + b);
        return super.generateLayoutParams(attrs);
    }

你可能感兴趣的:(总结,Android,ViewGroup,子视图)