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

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

Android button drawable does not fit the text

问题

// 创建一个新按钮
Button b = new Button(this);
b.setText(someText);
b.setId(id);
b.setAllCaps(false);
b.setTextColor(getResources().getColor(R.color.textOnButton));
b.setBackground(drawable);
b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        doAction(v);
    }
});

// 获取线性布局并设置参数
LinearLayout linear = findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, BUTTON_HEIGHT);
params.setMargins(LEFT_MARGIN_BUTTON, TOP_MARGIN_BUTTON, RIGHT_MARGIN_BUTTON, DOWN_MARGIN_BUTTON);
linear.setOrientation(LinearLayout.VERTICAL);

// 将按钮添加到线性布局中
linear.addView(b, params);

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

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

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


<details>
<summary>英文:</summary>

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:
```xml
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:shape=&quot;rectangle&quot;&gt;
    &lt;solid android:color=&quot;@color/colorPrimary&quot; /&gt;
    &lt;corners
        android:bottomRightRadius=&quot;24dp&quot;
        android:bottomLeftRadius=&quot;24dp&quot;
        android:topRightRadius=&quot;24dp&quot;
        android:topLeftRadius=&quot;24dp&quot;/&gt;
&lt;/shape&gt;

The button is defined programaticaly by:

//make new button
Button b = new Button(this);
b.setText(someText);
b.setId(id);
b.setAllCaps(false);
b.setTextColor(getResources().getColor(R.color.textOnButton));
b.setBackground(drawable);
b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        doAction(v);
    }
});

//setParameters of Linearlayout
LinearLayout linear = findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, BUTTON_HEIGHT);
params.setMargins(LEFT_MARGIN_BUTTON, TOP_MARGIN_BUTTON,RIGHT_MARGIN_BUTTON,DOWN_MARGIN_BUTTON);
linear.setOrientation(LinearLayout.VERTICAL);

//add button to linearlayout
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

使用:

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

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

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

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

英文:

Use:

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

In your case you can also use a simple MaterialButton:

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

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

答案2

得分: 0

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

//首先获取默认文本高度
int textSize = (int) new Button(this).getTextSize();

//然后将布局的高度设置为这个默认文本高度加上一些常数
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT,
     textSize + BUTTON_TEXT_MARGIN);
英文:

Another way to fit this purpose is to use:

//first acquire the default text height
int textSize = (int) new Button(this).getTextSize();

//then set the height of the layout to this default text height plus some constant
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT,
     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:

确定