【问题标题】:How to create a multiple column table view in android using java code?如何使用java代码在android中创建多列表视图?
【发布时间】:2011-08-30 03:34:03
【问题描述】:

我正在开发一个安卓应用程序。为此,我需要一个动态的 tableView。我将 TableLayout 用于 android 中可用的内容。但是我找不到在我的 tableView 中有多个列的方法。请问有什么办法吗?

【问题讨论】:

    标签: android tableview


    【解决方案1】:

    您可以在设计视图中添加任意数量的列。您所要做的就是将要显示的任何 View 元素放在 TablaRow 标记中。

    <TableLayout>
      <TableRow>
        <TextView></TextView> // That's a column
        <ImageView></ImageView>  // That's other column
        ....
    
        <Other views></Other views> // That's the last column
      </TableRow>
     </TableLayout>
    

    【讨论】:

      【解决方案2】:

      我不知道我是否完全理解你的问题,但在这里:

      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          TableLayout tableLayout = new TableLayout(getApplicationContext());
          TableRow tableRow;
          TextView textView;
      
          for (int i = 0; i < 4; i++) {
              tableRow = new TableRow(getApplicationContext());
              for (int j = 0; j < 3; j++) {
                  textView = new TextView(getApplicationContext());
                  textView.setText("test");
                  textView.setPadding(20, 20, 20, 20);
                  tableRow.addView(textView);
              }
              tableLayout.addView(tableRow);
          }
          setContentView(tableLayout);
      }
      

      此代码创建具有 3 列和 4 行的 TableLayout。基本上你可以在 XML 文件中声明 TableLayout,然后 setContentView 到 XML,然后使用 findViewById 来找到你的 TableLayout。只有 TableRow 及其子项必须在 java 代码中完成。

      【讨论】:

      • 没问题,如果您只需要这些,您应该考虑将此问题标记为“已回答”。
      猜你喜欢
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多