自定义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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值