如何获得在Android上的按钮的边框大小边框、如何获得、按钮、大小

2023-09-06 10:16:28 作者:缘来缘去浮生尽

我怎样才能得到一个独立的Andr​​oid编程按钮的边框宽度?我只需要调整文字大小,以适合灰色地带,需要用按钮列式位置的其他对象,但我不能这样做,不知道边框的大小。我需要这在所有的API 7+工作。下图中的红色箭头显示什么,我试图让:

How can I get the border width of a stand Android button programmatically? I simply need to resize the text to fit to the gray area and need to position other objects inline with the buttons, but I cannot do that without knowing the size of the border. I need this to work in all API's 7+. The red arrows in the image below show what I am trying to get:

下面是code我用我的创建按钮:

Here is the code I use for creating my button:

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。我已经因为有人downvoted我的问题最后一次再问这个问题,我绝望的解决方案。我在周取得绝对没有任何进展与我的计划,因为我一直在坚持这一点,另一个问题。如果有什么不清楚我的问题,请让我知道,我会编辑。谢谢

NB. I've had to ask this question again because someone downvoted my question last time and I am desperate for a solution. I have made absolutely no progress with my program in weeks because I have been stuck with this and another problem. If there is something unclear about my question, please let me know and I will edit it. Thank you

推荐答案

从意见我最初的code推荐可以发现的此处。丹布雷所做的实现是:

My initial code recommendation from the comments can be found here. The implementation done by Dan Bray is:

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