如何在Android中获取按钮的边框大小

时间:2013-06-28 23:14:12

标签: java android button size border

如何以编程方式获取支架Android按钮的边框宽度?我只需要调整文本大小以适应灰色区域,并且需要将其他对象与按钮内联在一起,但是如果不知道边框的大小,我就不能这样做。我需要这个在所有API的7+中工作。下图中的红色箭头显示了我想要获得的内容:

Border Width

以下是我用来创建按钮的代码:

cmdView = new Button(this);
params = new RelativeLayout.LayoutParams(widthLblViewVerbs , (int) fieldHeight);
params.leftMargin = (int) (screenWidth - params.width);
params.topMargin = (int) yPos;
cmdView.setSingleLine();
cmdView.setText("View");
cmdView.setPadding(0, 0, 0, 0);
cmdView.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(cmdView.getText().toString(), params.width, params.height));
layout.addView(cmdView, params);

NB。我不得不再次提出这个问题,因为上次有人对我的问题进行了低估,我迫切需要一个解决方案。几个星期以来,我的计划完全没有取得任何进展,因为我一直坚持这个和另一个问题。如果我的问题有些不清楚,请告诉我,我会对其进行编辑。谢谢

4 个答案:

答案 0 :(得分:1)

问题上并不是很清楚 - 你想要底层视图的边距然后设置按钮的大小来匹配?那你试过吗

ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

可以通过

访问边距
lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;

详细信息:http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

答案 1 :(得分:1)

这会获得按钮边框的宽度和高度:

final Button button = new Button(this);
params = new RelativeLayout.LayoutParams(50, 50);
layout.addView(button, params);
button.post(new Runnable()
{
    @Override
    public void run()
    {
        button.buildDrawingCache();
        Bitmap viewCopy = button.getDrawingCache();

        boolean stillBorder = true;
        PaddingLeft = 0;
        PaddingTop = 0;
        while (stillBorder)
        {
            int color = viewCopy.getPixel(PaddingLeft, button.getHeight() / 2);
            if (color != Color.TRANSPARENT)
                stillBorder = false;
            else
                PaddingLeft++;
        }              
        stillBorder = true;
        while (stillBorder)
        {
            int color = viewCopy.getPixel(button.getWidth() / 2, PaddingTop);
            if (color != Color.TRANSPARENT)
                stillBorder = false;
            else
                PaddingTop++;
        }
    }
});

答案 2 :(得分:1)

我可以找到评论中的初始代码建议here。 Dan Bray的实施是:

final Button button = new Button(this);
params = new RelativeLayout.LayoutParams(50, 50);
layout.addView(button, params);
button.post(new Runnable()
{
@Override
public void run()
{
    button.buildDrawingCache();
    Bitmap viewCopy = button.getDrawingCache();

    boolean stillBorder = true;
    PaddingLeft = 0;
    PaddingTop = 0;
    while (stillBorder)
    {
        int color = viewCopy.getPixel(PaddingLeft, button.getHeight() / 2);
        if (color != Color.TRANSPARENT)
            stillBorder = false;
        else
            PaddingLeft++;
    }              
    stillBorder = true;
    while (stillBorder)
    {
        int color = viewCopy.getPixel(button.getWidth() / 2, PaddingTop);
        if (color != Color.TRANSPARENT)
            stillBorder = false;
        else
            PaddingTop++;
    }
}
});

答案 3 :(得分:0)

假设您正在使用九个补丁背景图像,则有一种更简单的方法。

在九个补丁图像中,您可以定义填充框。此填充框将自动应用于您设置的内容。在这种情况下,无需手动重新计算填充。

请注意,您不应使用九个补丁作为背景在视图上设置填充,因为九个补丁中的填充将被忽略。