【发布时间】:2015-10-26 14:43:09
【问题描述】:
我正在使用 Android Studio 中的循环动态创建按钮。然而,这些按钮相互重叠。目前我已将循环设置为运行一定次数,最终我将使用数据库中可能会有所不同的数字。我正在尝试设置这些按钮,以便无论按钮数量如何,它们总是水平居中。包括吐司,因为我看不到是否创建了多个按钮,因为它们都在彼此之上。
LinearLayout layout = (LinearLayout)findViewById(R.id.space_layout);
for (int i = 0; i <= 3; i++ ){
Button myButton = new Button(this);// initialization of new button
myButton.setText("");
myButton.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(myButton);
Toast.makeText(this, "Button Created" + i, Toast.LENGTH_SHORT).show();
}
【问题讨论】:
-
我一直在问自己为什么人们喜欢以编程方式构建他们的 UI...
-
我不明白。为什么在 LinearLayout 中使用 RelativeLayout 参数?
-
当您有少量/大量按钮时,您希望它看起来如何?!
标签: android android-layout android-studio android-button dynamically-generated