【问题标题】:How to get the button's border size in Android如何在Android中获取按钮的边框大小
【发布时间】:2013-06-26 19:59:51
【问题描述】:

如何以编程方式获取站立式 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);

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

【问题讨论】:

  • 你试过改变填充吗?
  • 是的,我试过了,但它不起作用
  • 你能贴出你用来创建按钮的代码吗?
  • 我已将生成按钮的代码从评论移至问题的一部分
  • 我认为您正在寻找的是构成Button 状态的drawable 中的高亮部分。看看这个gist.github.com/luksprog/5940136。此外,发布问题并获得反对票(或急需回复)并不是重新发布问题的理由,始终改进第一个问题并提供更多详细信息(和/或赏金)。跨度>

标签: java android button size border


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我在 cmets 的初始代码推荐可以在 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】:

      获取按钮边框的宽度和高度:

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

      【讨论】:

        【解决方案4】:

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

        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

        【讨论】:

        • 我添加了这 4 行代码:View view = (View) cmdView; ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); paddingLeft = lp.leftMargin; paddingTop = lp.topMargin; 不幸的是,它阻止了我的程序启动。我基本上是想获得我发布的图片上红色箭头的长度
        猜你喜欢
        • 1970-01-01
        • 2013-05-31
        • 1970-01-01
        • 2013-04-21
        • 2015-09-16
        • 2011-03-21
        • 2015-01-30
        • 2022-01-23
        • 2023-04-09
        相关资源
        最近更新 更多