在这个,这个和这个线程中,我试图找到一个关于如何在单个视图上设置边缘的答案。然而,我想知道是否有更简单的方法。我将解释为什么我不想使用这种方法:

我有一个自定义按钮扩展按钮。如果背景被设置为默认背景以外的东西(通过调用setBackgroundResource(int id)或setBackgroundDrawable(Drawable d)),我希望边缘为0。如果我调用这个:

public void setBackgroundToDefault() {
    backgroundIsDefault = true;
    super.setBackgroundResource(android.R.drawable.btn_default);
    // Set margins somehow
}

我希望边距重置为-3dp(我已经在这里读到如何从像素转换为dp,所以一旦我知道如何设置px边距,我可以自己管理转换)。但是由于这是在CustomButton类中调用的,父类可以从LinearLayout到TableLayout变化,我宁愿不让他获得他的父类并检查该父类的实例。我想那也不太合适。

另外,当调用(使用LayoutParams) parentLayout。addView(myCustomButton, newParams),我不知道这是否将它添加到正确的位置(但是还没有尝试过),说一行中的五个按钮。

问:除了使用LayoutParams,还有什么更简单的方法来编程设置单个按钮的边距吗?

编辑:我知道的LayoutParams方式,但我想要一个解决方案,避免处理每个不同的容器类型:

ViewGroup.LayoutParams p = this.getLayoutParams();
    if (p instanceof LinearLayout.LayoutParams) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }
    else if (p instanceof RelativeLayout.LayoutParams) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }
    else if (p instanceof TableRow.LayoutParams) {
        TableRow.LayoutParams lp = (TableRow.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }
}

因为this.getLayoutParams();返回一个ViewGroup。LayoutParams,它们没有属性topMargin, bottomMargin, leftMargin, rightMargin。 你看到的mc实例只是一个MarginContainer,它包含偏移量(-3dp)边距和(oml, omr, omt, omb)和原始边距(ml, mr, mt, mb)。


当前回答

对我来说,我使用的是ViewGroup。themedbutton组件中的LayoutParams。使用ViewGroup。LayoutParams不能使用setMargin。

相反,使用MarginLayoutParams如下,它为我工作。

ViewGroup.MarginLayoutParams params =new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        params.setMargins(0,8,0,0);

其他回答

使用Android KTX,你可以这样做:

yourView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
   setMargins(0, 0, 0, 0)
}

使用芬兰湾的科特林,

        yourLayoutId.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            setMargins(15,15,15,15)
        }

在Kotlin中,它看起来像这样:

val layoutParams = (yourView?.layoutParams as? MarginLayoutParams)
layoutParams?.setMargins(40, 40, 40, 40)
yourView?.layoutParams = layoutParams

这个方法可以让你在DP中设置Margin

public void setMargin(Context con,ViewGroup.LayoutParams params,int dp) {

        final float scale = con.getResources().getDisplayMetrics().density;
        // convert the DP into pixel
        int pixel =  (int)(dp * scale + 0.5f); 

        ViewGroup.MarginLayoutParams s =(ViewGroup.MarginLayoutParams)params;
        s.setMargins(pixel,pixel,pixel,pixel);

        yourView.setLayoutParams(params);
}

更新

您可以根据需要更改参数。

Layout_margin是子视图告诉父视图的约束。然而,是否允许保证金是由母公司决定的。基本上通过设置android:layout_margin="10dp",子视图请求父视图组分配比其实际大小大10dp的空间。(另一方面,padding="10dp"意味着子视图将自己的内容缩小10dp。)

因此,并非所有viewgroup都尊重边缘。最臭名昭著的例子是listview,其中项的边距被忽略。在你调用setMargin()到一个LayoutParam之前,你应该始终确保当前视图位于一个支持margin的ViewGroup中(例如LinearLayouot或RelativeLayout),并将getLayoutParams()的结果转换为你想要的特定的LayoutParams。(ViewGroup。LayoutParams甚至没有setedges()方法!)

下面的函数应该可以做到这一点。但是,请确保将RelativeLayout替换为父视图的类型。

private void setMargin(int marginInPx) {
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
    lp.setMargins(marginInPx,marginInPx, marginInPx, marginInPx);
    setLayoutParams(lp);
}