Android按钮的可绘制部分与文本不匹配

huangapple go评论98阅读模式
英文:

Android button drawable does not fit the text

问题

  1. // 创建一个新按钮
  2. Button b = new Button(this);
  3. b.setText(someText);
  4. b.setId(id);
  5. b.setAllCaps(false);
  6. b.setTextColor(getResources().getColor(R.color.textOnButton));
  7. b.setBackground(drawable);
  8. b.setOnClickListener(new View.OnClickListener() {
  9. public void onClick(View v) {
  10. doAction(v);
  11. }
  12. });
  13. // 获取线性布局并设置参数
  14. LinearLayout linear = findViewById(R.id.linearLayout);
  15. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  16. LinearLayout.LayoutParams.MATCH_PARENT, BUTTON_HEIGHT);
  17. params.setMargins(LEFT_MARGIN_BUTTON, TOP_MARGIN_BUTTON, RIGHT_MARGIN_BUTTON, DOWN_MARGIN_BUTTON);
  18. linear.setOrientation(LinearLayout.VERTICAL);
  19. // 将按钮添加到线性布局中
  20. linear.addView(b, params);

但是,如果我使用此可绘制对象作为按钮的背景,它会显示为:
Android按钮的可绘制部分与文本不匹配

中间的白色混乱部分是一些文本,应该显示为类似于这样的内容:
Android按钮的可绘制部分与文本不匹配

理想情况下,我希望将按钮的高度更改为系统默认文本大小加上一些间距。有办法解决这个问题吗?

  1. <details>
  2. <summary>英文:</summary>
  3. For my app I want to add buttons to a linearlayout programmatically with a background to make it look better. For the background I use a drawable. The drawable is defined as:
  4. ```xml
  5. &lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  6. android:shape=&quot;rectangle&quot;&gt;
  7. &lt;solid android:color=&quot;@color/colorPrimary&quot; /&gt;
  8. &lt;corners
  9. android:bottomRightRadius=&quot;24dp&quot;
  10. android:bottomLeftRadius=&quot;24dp&quot;
  11. android:topRightRadius=&quot;24dp&quot;
  12. android:topLeftRadius=&quot;24dp&quot;/&gt;
  13. &lt;/shape&gt;

The button is defined programaticaly by:

  1. //make new button
  2. Button b = new Button(this);
  3. b.setText(someText);
  4. b.setId(id);
  5. b.setAllCaps(false);
  6. b.setTextColor(getResources().getColor(R.color.textOnButton));
  7. b.setBackground(drawable);
  8. b.setOnClickListener(new View.OnClickListener() {
  9. public void onClick(View v) {
  10. doAction(v);
  11. }
  12. });
  13. //setParameters of Linearlayout
  14. LinearLayout linear = findViewById(R.id.linearLayout);
  15. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  16. LinearLayout.LayoutParams.MATCH_PARENT, BUTTON_HEIGHT);
  17. params.setMargins(LEFT_MARGIN_BUTTON, TOP_MARGIN_BUTTON,RIGHT_MARGIN_BUTTON,DOWN_MARGIN_BUTTON);
  18. linear.setOrientation(LinearLayout.VERTICAL);
  19. //add button to linearlayout
  20. linear.addView(b, params);

However, if I make a button with this drawable as background, it will appear as:
Android按钮的可绘制部分与文本不匹配

The white mess in the middle is some text that should appear as something like this:
Android按钮的可绘制部分与文本不匹配

Idealy I would like to change the height of the button to the default textsize of the system plus some margin. Is there way to fix this?

答案1

得分: 1

使用:

  1. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  2. LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

在你的情况下,你也可以使用一个简单的 MaterialButton

  1. val b = MaterialButton(this)
  2. b.cornerRadius = resources.getDimensionPixelOffset(R.dimen.cornerSize)

Android按钮的可绘制部分与文本不匹配

英文:

Use:

  1. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  2. LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

In your case you can also use a simple MaterialButton:

  1. val b = MaterialButton(this)
  2. b.cornerRadius = resources.getDimensionPixelOffset(R.dimen.cornerSize)

Android按钮的可绘制部分与文本不匹配

答案2

得分: 0

另一种实现这个目的的方法是使用:

  1. //首先获取默认文本高度
  2. int textSize = (int) new Button(this).getTextSize();
  3. //然后将布局的高度设置为这个默认文本高度加上一些常数
  4. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  5. LinearLayout.LayoutParams.MATCH_PARENT,
  6. textSize + BUTTON_TEXT_MARGIN);
英文:

Another way to fit this purpose is to use:

  1. //first acquire the default text height
  2. int textSize = (int) new Button(this).getTextSize();
  3. //then set the height of the layout to this default text height plus some constant
  4. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  5. LinearLayout.LayoutParams.MATCH_PARENT,
  6. textSize + BUTTON_TEXT_MARGIN);

huangapple
  • 本文由 发表于 2020年8月22日 04:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63529914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定