1

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

边框宽度

这是我用于创建按钮的代码:

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);

注意。我不得不再次问这个问题,因为上次有人对我的问题投了反对票,我迫切需要一个解决方案。几周来我的程序完全没有进展,因为我一直被这个和另一个问题所困扰。如果我的问题有什么不清楚的地方,请告诉我,我会编辑它。谢谢

4

4 回答 4

1

我从评论中推荐的初始代码可以在这里找到。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++;
    }
}
});
于 2013-07-08T11:29:44.937 回答
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++;
        }
    }
});
于 2013-07-08T03:51:20.127 回答
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

于 2013-06-29T01:56:15.193 回答
0

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

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

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

于 2013-07-08T14:14:03.503 回答