Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在初始化时在onLayout中向CustomView添加边距_Android_Android Custom View_Android Framelayout - Fatal编程技术网

Android 在初始化时在onLayout中向CustomView添加边距

Android 在初始化时在onLayout中向CustomView添加边距,android,android-custom-view,android-framelayout,Android,Android Custom View,Android Framelayout,我有一个扩展FrameLayout的自定义视图: public SlidingDrawer(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SlidingDrawer(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleA

我有一个扩展FrameLayout的自定义视图:

    public SlidingDrawer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlidingDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //get the attributes specified in attrs.xml using the name we included
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.SlidingLayer, 0, 0);

        init = true;
    }
我想在customView中为布局添加一些边距:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        super.onLayout(changed, l, t, r, b);

        if(init) { 
             init = false;

             MarginLayoutParams margins = MarginLayoutParams.class.cast(getLayoutParams());
             final View parent = (View) getParent();
             params.bottomMargin = mOffsetDistance - getHeight();
                    params.topMargin = parent.getHeight()  - mOffsetDistance ;
             setLayoutParams(margins);
        }
    }
这些利润将永远不适用。
有人能给我解释一下解决方案吗?

关于
MarginLayoutParams
有什么特别的吗?顺便说一句,这些巨大的任意边距是什么?我也使用了set/getLayoutParams和同样的问题。我用正确的边距值编辑了这个问题。它们只是一个示例。只是一个瞎猜,可能是因为调用了第一个构造函数?