gpt4 book ai didi

android - 如何在 Android 中以编程方式更改按钮大小?

转载 作者:太空狗 更新时间:2023-10-29 16:26:23 26 4
gpt4 key购买 nike

我正在创建按钮并以编程方式将它们添加到 LinearLayout。但是,我正在努力更改按钮大小。在下面的代码中,无论我在 deleteBtn.setWidth(num) 和 deleteBtn.setHeight(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);
}
}

我应该写些什么才能使按钮大小发生变化?

最佳答案

您应该应用包含您的ButtonLinearLayoutLayoutParams

deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(5, 5));

关于android - 如何在 Android 中以编程方式更改按钮大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51077240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com