如何在 Android 中以编程方式更改按钮大小?
Posted
技术标签:
【中文标题】如何在 Android 中以编程方式更改按钮大小?【英文标题】:How to change button size programmatically in Android? 【发布时间】:2018-06-28 07:34:49 【问题描述】:我正在创建按钮并以编程方式将它们添加到 LinearLayout。但是,我正在努力更改按钮大小。在下面的代码中,无论我在 deleteBtn.setWidth(num) 和 deleteBtn.setHeight(num) 中输入什么 num,都没有任何变化。
private void populateHorizontalLayouts(CustomMessageEvent event)
// Need to remove all views each time an user adds a number
// so that the same number is not rendered multiple times.
nameAndNumbersLayout.removeAllViews();
//displayedNamesAndNumbers.add(event.getCustomMessage());
for(int i =0; i < displayedNamesAndNumbers.size(); i++)
String displayedNumberAndName = displayedNamesAndNumbers.get(i);
LinearLayout horizontalLayout = new LinearLayout(view.getContext());
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setId(i);
Button deleteBtn = new Button(view.getContext());
deleteBtn.setId(i);
deleteBtn.setText("Delete");
deleteBtn.setWidth(5);
deleteBtn.setHeight(5);
TextView nameAndNumView = new TextView(view.getContext());
nameAndNumView.setId(i);
nameAndNumView.setText(displayedNumberAndName);
horizontalLayout.addView(deleteBtn);
horizontalLayout.addView(nameAndNumView);
nameAndNumbersLayout.addView(horizontalLayout);
我应该写什么来改变按钮大小?
【问题讨论】:
Programmatically change the width of the Button in android的可能重复 【参考方案1】:您应该应用包含您的Button
的LinearLayout
中的LayoutParams
。
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
【讨论】:
对于 Kotlin 程序员来说,它看起来像这样:deleteBtn.layoutParams = LinearLayout.LayoutParams(5, 5)
【参考方案2】:
试试这个
deleteBtn.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT, 15));
【讨论】:
【参考方案3】:尝试以下方法:
int pixels = Math.round(convertDpToPixel(50, getApplicationContext()));
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(pixels, pixels));
public static float convertDpToPixel(float dp, Context context)
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
【讨论】:
【参考方案4】:您可以使用 LayoutParams 设置宽度和高度,通常当我使用尺寸进行管理时,我将其用作参考屏幕的尺寸。 我经常做这样的事情:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
float targetWidth = width / 3;
float targetHeight = targetWidth * 3;
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(targetWidth, targetHeight);
【讨论】:
【参考方案5】:如果你只是想改变高度并保持宽度不变,
// This sets the height to 5 *pixels*, with keeping the width as it is.
deleteBtn.setLayoutParams (new LayoutParams(deleteBtn.getLayoutParams().getWidth(), 5));
只改变宽度,
deleteBtn.setLayoutParams (new LayoutParams(5, deleteBtn.getLayoutParams().getHeight()));
【讨论】:
【参考方案6】:搜索要在表格行 [tbrow] 中显示的按钮 [del] 的答案,结果只设置了按钮出现的表格行的参数。
del = new Button(getApplicationContext());
del.setText("DELETE");
del.setTextColor(Color.BLACK);
del.setGravity(Gravity.END);
del.setBackgroundColor(Color.parseColor("#FFFFC107"));
del.setLayoutParams (new TableRow.LayoutParams(80, 50));
tbrow.addView(del);
【讨论】:
以上是关于如何在 Android 中以编程方式更改按钮大小?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UIAlertController 中以编程方式更改特定按钮文本颜色