2013-06-28 35 views
1

如何以编程方式获取支架Android按钮的边框宽度?我只需要调整文本的大小以适合灰色区域,并需要将其他对象与按钮对齐放置,但如果不知道边框的大小,我无法做到这一点。我需要这个在所有API的7+中工作。下图中的红色箭头显示什么,我试图让:如何在Android中获取按钮的边框尺寸

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。我不得不再次提出这个问题,因为上次有人低估了我的问题,我急于寻求解决方案。我已经在几周内完成了我的计划,因为我一直坚持这个和另一个问题。如果我的问题有些不清楚,请让我知道,我会编辑它。谢谢

+0

你有没有试过改变填充? – AedonEtLIRA

+0

是的,我试过了,它不起作用 –

+0

你可以发布你用来创建按钮的代码吗? – AedonEtLIRA

回答

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

+0

我已经添加了这4行代码: 'View view =(View)cmdVi​​ew; ViewGroup。MarginLayoutParams lp =(ViewGroup.MarginLayoutParams)view.getLayoutParams(); paddingLeft = lp.leftMargin; paddingTop = lp.topMargin;' 不幸的是,它阻止了我的程序启动。我基本上试图获得我张贴的图像上的红色箭头的长度 –

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

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

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

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