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

3

我如何在继承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>

这是您层次结构的根布局吗? - Blackbelt
1
@WojciechKo 为什么你需要使用边距?自定义的“View”设置自己的边距似乎很奇怪。通常这取决于“View”所在的布局。那么为什么不能使用填充?而且你首先要做什么呢?边距并不总是有效,有些“Views”会忽略它们。 - Xaver Kapeller
@WojciechKo 那么,有什么阻止你在xml中定义边距的呢? - Xaver Kapeller
@XaverKapeller,在自定义视图中无法设置android:layout_margin的默认值吗? - WojciechKo
@WojciechKo 你所尝试做的没有任何问题,我只是想说你不能使用边距实现它。应该使用填充来实现,详情请见我的答案。 - Xaver Kapeller
显示剩余10条评论
1个回答

1

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

通过使用内边距和一个子View,您可以在本质上复制边距的效果,而不会带来任何与边距相关的问题:

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 视图应包含您想要在自定义 View 中显示的所有内容。


我需要MyRelativeLayout的内容在xml文件中定义。您的答案能实现这个吗? - WojciechKo
需要进行一些修改,但是可以实现。为了实现这个,你需要重写 addViewToLayout() 方法,移除 super 调用,并将 Views 添加到 innerView 中。你是否遇到了背景或类似的问题?如果没有,那么你可以简单地使用 padding。如果有,那么你还应该考虑定义一个额外的布局来放置内容,并将此布局填充到 innerView 中。有许多方法可以解决这个问题。 - Xaver Kapeller
你能帮我解决这个问题吗:http://stackoverflow.com/questions/24680131/custom-scrollable-layout-with-default-values 我花了很多时间在这上面,但是完全不知道该怎么做。我的所有想法都行不通。 - WojciechKo
我会看一下,但请给我一些时间,我现在正在回家的路上。 - Xaver Kapeller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接