如何使用java代码在android中创建多列表视图?

hig*_*ive 3 android tableview

我正在开发一个Android应用程序.为此,我需要一个动态的tableView.我正在使用TableLayout来获取android中可用的内容.但我找不到在tableView中有多个列的方法.有什么选择吗?

kam*_*ych 16

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

@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);
}
Run Code Online (Sandbox Code Playgroud)

此代码创建包含3列和4行的TableLayout.基本上你可以在XML文件中声明TableLayout,然后将setContentView声明为XML,并使用findViewById找到你的TableLayout.只有TableRow和它的子代必须在java代码中完成.