【问题标题】:Help: 8 X 10 2-dimensional array of buttons in android帮助:android 中的 8 X 10 二维按钮数组
【发布时间】:2010-03-23 09:25:04
【问题描述】:

我遇到了在 android 中动态创建按钮的问题。这就是我想做的-

我想创建 8 x 10 的按钮阵列。由于在 main.xml 中声明 80 个按钮效率不高,我想在程序本身中执行此操作。最大的问题是像网格一样放置/对齐按钮。我可以创建按钮对象,但如何在程序中对齐它们?

Button b = new Button(this); 
b.setId(i);
b.setText("Button " + i); 

像这样-

1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
.
.
.
10 10 10 10 10 10 10 10 10 10

任何“以编程方式”执行此操作的帮助将不胜感激

【问题讨论】:

  • 哇!你用的是什么设备,它的屏幕大到可以显示 80 个可点击按钮?
  • @Matt Lacey:软键盘大约有 10-11 个键宽,具体取决于布局。 OP 想要适合 8,所以相比之下这听起来并不合理。和 10 个按钮的高度应该更容易。
  • 屏幕上有 80 个按钮的想法仍然为我敲响了可用性和设计警钟。

标签: java android user-interface button


【解决方案1】:

您需要一个容器将它们全部放入:

<LinearLayout
    android:id="@+id/llContainer"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

然后我会将它们添加为 10 个单独的“行”:

LinearLayout container = (LinearLayout) findViewById(R.id.llContainer);

for(int i = 0; i < 10; i++) {
   LinearLayout row = new LinearLayout(this, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
   container.addView(row);

   for(int x = 0; x < 8; x++) {

      Button btn = new Button(this, new LayouParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      btn.setText(i + ':' + x);

      row.addView(btn);

   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2011-07-05
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多