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

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

问题描述

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

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();返回一个ViewGroup.LayoutParams,它没有topMarginbottomMarginleftMarginrightMargin.您看到的 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天全站免登陆