在Android中,如何以编程方式在dp中设置页边距?

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

我有一个自定义按钮,它扩展了Button。 如果背景设置为默认背景以外的其他东西(通过调用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不同,我不希望他得到他的父类并检查该父类的instanceof。 我想,这也会很不起作用。

另外,当调用(使用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 ,它没有属性topMarginbottomMarginleftMarginrightMargin 。 您所看到的MC实例只是一个MarginContainer包含偏移(-3dp)利润率和(OML,OMR,OMT,OMB)和原来的利润(ML,MR,MT,MB)。


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

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

根据您使用的布局,您应该使用RelativeLayout.LayoutParamsLinearLayout.LayoutParams

为了将你的DP指标转换为像素,请尝试以下操作:

Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        yourdpmeasure, 
        r.getDisplayMetrics()
);

LayoutParams - 不工作! ! !

需要使用类型:MarginLayoutParams

MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;

MarginLayoutParams的代码示例:

http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams


最好的方式:

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        view.requestLayout();
    }
}

如何调用方法:

setMargins(mImageView, 50, 50, 50, 50);

希望这会帮助你。

链接地址: http://www.djcxy.com/p/93291.html

上一篇: In Android, how do I set margins in dp programmatically?

下一篇: how to change programmatically?