Android 如何在继承自RelativeLayout的类中设置边距

Android 如何在继承自RelativeLayout的类中设置边距,android,layout,margin,Android,Layout,Margin,如何从RelativeLayout设置类扩展中的页边距? 我尝试过这个,但不起作用: public class MyRelativeLayout extends RelativeLayout { int margin = 50; public MyRelativeLayout(Context context) { super(context); setLook(); } public MyRelativeLayout(Conte

如何从RelativeLayout设置类扩展中的页边距?
我尝试过这个,但不起作用:

public class MyRelativeLayout extends RelativeLayout {
    int margin = 50;

    public MyRelativeLayout(Context context) {
        super(context);
        setLook();
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLook();
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLook();
    }

    private void setLook() {
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(margin, margin, margin, margin);
        setLayoutParams(params);
    }
}
我该怎么做

更新: 此视图的用法:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <info.korzeniowski.widget.MyRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    //content
    </info.korzeniowski.widget.MyRelativeLayout>
</ScrollView>

//内容

自定义的
视图应从不定义自己的页边距。页边距仅用于布局,您无法可靠地使用它们来设计自定义的
视图

通过使用填充和子视图,您基本上可以复制页边距的效果,而不会出现任何与页边距相关的问题:

public class MyRelativeLayout extends RelativeLayout {

    private final int padding;

    public MyRelativeLayout(Context context) {
        super(context);

        // Use 50 dip instead of 50 pixels
        this.padding = LayoutHelper.dpToPixel(context, 50);

        setLook();
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Use 50 dip instead of 50 pixels
        this.padding = LayoutHelper.dpToPixel(context, 50);

        setLook();
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // Use 50 dip instead of 50 pixels
        this.padding = LayoutHelper.dpToPixel(context, 50);

        setLook();
    }

    private void setLook() {
        setPadding(this.padding, this.padding, this.padding, this.padding);

        final View innerView = ...;
        final LayoutParams innerViewParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(innerView, innerViewParams);
    }
}

名为
innerView
视图应包含您希望在自定义
视图中显示的所有内容

它不起作用是什么意思?您可能会混淆边距和填充吗?请解释到底是什么不起作用。@blackbelt表示它没有作用。@XaverKapeller我没有,当我使用setPadding()时,它可以正常工作。我需要使用边距,但我不知道如何使用。@WojciechKo为什么需要使用边距?自定义
视图
设置了自己的边距,这似乎很奇怪。通常这取决于
视图
所在的布局。那你为什么不能用填充物呢?你一开始想做什么?页边距并不总是有效的,它们被一些
视图忽略。我需要在
xml
文件中定义
MyRelativeLayout
的内容。我能用你的答案做到这一点吗?它需要稍加修改,但可以。要实现这一点,您必须覆盖
addViewToLayout()
删除超级调用,并将
Views
添加到
innerView
。你在背景方面有困难吗?因为如果不是的话,你可以简单地用一个填充物。如果是,你也应该考虑为内容定义一个额外的布局,并将这个布局扩展到有很多方法可以解决这个问题。你能帮我解决这个问题吗:我花了很多时间在这个问题上,却不知道怎么做。我的想法都不管用。我会看一看,但给我一些时间,我现在正在回家的路上。