【问题标题】:TableLayout row and column in android with java带有java的android中的TableLayout行和列
【发布时间】:2014-03-28 18:53:41
【问题描述】:

我用java在android中设计了一个动态表。该表有 3 列,但最后一列显示屏幕外/外的数据,这些数据在屏幕上不可见,但存在。


如何让第三列的数据继续显示在其下方,并使所有数据对用户可见?


这是用于实现该目标的 java 代码:

LinearLayout main_layout = (LinearLayout) findViewById(R.id.main_layout);

    TableLayout table_service = new TableLayout(this);
    table_service.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    for(ServiceArray s : arr_service){

        TableRow tr_item = new TableRow(this);
        tr_item.setLayoutParams(new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT
                ));

        TextView date = new TextView(this);
        date.setText(s.getDateOfService());
        date.setTextColor(Color.WHITE);
        date.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT
                ));
        date.setBackgroundColor(Color.RED);

        TextView mileage = new TextView(this);
        mileage.setText(s.getMileage());
        mileage.setTextColor(Color.WHITE);
        mileage.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT
                ));
        mileage.setBackgroundColor(Color.BLUE);

        TextView description = new TextView(this);
        description.setText(s.getDescription());
        description.setTextColor(Color.WHITE);
        description.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT
                ));
        description.setBackgroundColor(Color.GREEN);

        tr_item.addView(date);
        tr_item.addView(mileage);
        tr_item.addView(description);

        table_service.addView(tr_item);
    }


    main_layout.addView(table_service);
    setContentView(main_layout);

xml在这里:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".DisplayMessageActivity" >

</LinearLayout>

图片here

【问题讨论】:

  • 你能分享你的xml布局吗?
  • Iv 更新了代码 Daniel.. 事实上我需要跨越行并将第三个数据放入单元格本身并避免数据被隐藏在屏幕之外。

标签: java android android-layout android-linearlayout android-tablelayout


【解决方案1】:

看看setColumnShrinkable()official doc。这将缩小您的列以使其适合屏幕。

此外,如果设备处于横向模式,请查看 setColumnStrechable()official doc 以使列拉伸(因为列可能不够宽)。让我知道它是如何工作的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多