在Android中,如何以编程方式在dp中设置边距? [英] In Android, how do I set margins in dp programmatically?

查看:139
本文介绍了在Android中,如何以编程方式在dp中设置边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

我有一个自定义的Button,它扩展了Button.如果背景设置为默认背景以外的其他值(通过调用setBackgroundResource(int id)setBackgroundDrawable(Drawable 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(我已经在此处如何从像素转换为dp,因此,一旦我知道如何设置px的边距,就可以自己管理转换了.但是由于这是在CustomButton类中调用的,因此父级可以从LinearLayout到TableLayout有所不同,而我宁愿不要让他获得其父级并检查该父级的实例.我想那也将表现不佳.

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以外,还有没有其他更简便的方法以编程方式设置单个Button的边距?

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();返回不具有属性topMarginbottomMarginleftMarginrightMarginViewGroup.LayoutParams. 您看到的mc实例只是一个MarginContainer,其中包含偏移量(-3dp)边距和(oml,omr,omt,omb)和原始边距(ml,mr,mt,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.LayoutParamsLinearLayout.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()
);

这篇关于在Android中,如何以编程方式在dp中设置边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆