在Android中,如何在DP编程设置页边距如何在、Android、页边距、DP

2023-09-12 02:42:54 作者:谢幕的情戏,显得好迷离

在this, 这和this线程我试图找到如何设置页边距在一个单一视图的答案。不过,我在想,如果没有一个更简单的方法。我会解释为什么我宁愿不希望用这种方式:

In this, this and this thread I tried to find an answer on how to set the margins on a single view. However, I was wondering if there isn't an easier way. I'll explain why I rather wouldn't want to use this approach:

我有延伸按钮自定义按钮。如果背景设置为别的东西比默认的背景(通过调用 setBackgroundResource(INT ID) setBackgroundDrawable(可绘制D)),我想边距为0。如果我把这称为:

I have a custom Button which extends Button. If the background is set to something else than the default background (by calling either setBackgroundResource(int id) or setBackgroundDrawable(Drawable d)), I want the margins to be 0. If I call this:

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

我要边距重置为-3dp(我已经读过here如何从像素转换为DP,所以一旦我知道如何在PX设置页边距,我可以管理转换自己)。但由于这是所谓的的CustomButton 班,家长可以从的LinearLayout变化到TableLayout,我宁愿没有他得到他的父母和检查的instanceof的父母。这也将是相当inperformant,我想。

I want the margins to reset to -3dp (I already read here how to convert from pixels to dp, so once I know how to set margins in px, I can manage the conversion myself). But since this is called in the CustomButton class, the parent can vary from LinearLayout to TableLayout, and I'd rather not have him get his parent and check the instanceof that parent. That'll also be quite inperformant, I imagine.

此外,调用(使用的LayoutParams) parentLayout.addView(myCustomButton,newParams),我不知道这是否将其添加到正确的位置的时候(有没有但是试过),说一排五个中间的按钮。

Also, when calling (using LayoutParams) parentLayout.addView(myCustomButton, newParams), I don't know if this adds it to the correct position (haven't tried however), say the middle button of a row of five.

问:有没有更简单的方法来设置一个按钮的边缘编程的除了使用的LayoutParams

Question: Is there any easier way to set the margin of a single Button programmatically besides using LayoutParams?

在此先感谢。

编辑:我知道的的LayoutParams的方式,但我想这避免了处理各种不同容器类型的解决方案:

I know of the LayoutParams way, but I'd like a solution that avoids handling each different container type:

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)和原来的利润(毫升,先生,山,MB)。

Because this.getLayoutParams();returns a ViewGroup.LayoutParams, which do not have the attributes topMargin, bottomMargin, leftMargin, rightMargin. The mc instance you see is just a MarginContainer which contains offset (-3dp) margins and (oml, omr, omt, omb) and the original margins (ml, mr, mt, mb).

推荐答案

您应该使用的LayoutParams 来设置按钮边距:

You should use LayoutParams to set your button margins:

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);

根据您所使用的布局你应该使用 RelativeLayout.LayoutParams LinearLayout.LayoutParams

Depending on what layout you're using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams.

和你的DP措施转化为像素,试试这个:

And to convert your dp measure to pixel, try this:

Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        yourdpmeasure, 
        r.getDisplayMetrics()
);
 
精彩推荐
图片推荐