英文:
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);
理想情况下,我希望将按钮的高度更改为系统默认文本大小加上一些间距。有办法解决这个问题吗?
<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
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
<corners
android:bottomRightRadius="24dp"
android:bottomLeftRadius="24dp"
android:topRightRadius="24dp"
android:topLeftRadius="24dp"/>
</shape>
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:
The white mess in the middle is some text that should appear as something like this:
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)
英文:
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)
答案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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论