【问题标题】:How to set Margins in class inherited from RelativeLayout如何在继承自RelativeLayout的类中设置边距
【发布时间】:2014-08-31 21:49:50
【问题描述】:

如何在从 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 这意味着它不起作用。
  • @XaverKapeller 我不是,当我使用 setPadding() 它工作正常。我需要使用边距,但我不知道如何使用。
  • @WojciechKo 为什么需要使用边距?自定义 View 设置它自己的边距似乎很奇怪。通常这取决于View 的布局。那么为什么不能使用填充呢?你首先想做什么?边距并不总是有效,它们被一些Views 忽略。

标签: android layout margin


【解决方案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);
    }
}

名为innerViewView 应该包含您要在自定义View 中显示的所有内容。

【讨论】:

  • 我需要那个,MyRelativeLayout 的内容要定义在xml 文件中。我可以通过您的回答实现这一目标吗?
  • 它需要一点修改,但是可以。要实现这一点,您必须覆盖addViewToLayout() 删除超级调用并将Views 添加到innerView。你有没有背景或类似的问题?因为如果不是,您可以简单地使用填充。如果是,那么您还应该考虑为内容定义一个额外的布局并将此布局扩展为innerView。有很多方法可以解决这个问题。
  • 你能帮我解决这个问题吗:stackoverflow.com/questions/24680131/…我花了很多时间在这个问题上,但不知道该怎么做。我的想法都不行。
  • 我去看看,但给我一些时间,我现在正在回家的路上。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2012-04-06
  • 1970-01-01
  • 2011-05-19
  • 2022-10-21
相关资源
最近更新 更多