【问题标题】:Setting table layout through Java dynamically通过Java动态设置表格布局
【发布时间】:2014-06-08 19:57:16
【问题描述】:

我需要我的屏幕在活动布局中定义几个文本视图。在这些视图下方,我想设置一个表,其行数和列数将取决于用户的输入(动态)。

这段代码在activity的oncreate中

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    TableLayout tableLayout = new TableLayout(getApplicationContext());
    TableRow tableRow;
    TextView textView;

    for (int i = 0; i < no_days; i++) {
        tableRow = new TableRow(getApplicationContext());
        for (int j = 0; j < no_subjects; j++) {
            textView = new TextView(getApplicationContext());
            textView.setText("Hello");
            textView.setPadding(20, 20, 20, 20);
            tableRow.addView(textView);
        }
        tableLayout.addView(tableRow);
    }

    setContentView(tableLayout);

但是这段代码占据了整个屏幕,我无法拥有我在活动布局中定义的那些文本视图。

这是我的 activity_home_page.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame" >

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/welcome"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/display_user_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView5"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

【问题讨论】:

  • 发布您的活动主页
  • 是 activity_home_page.xml

标签: java android dynamic android-tablelayout


【解决方案1】:

如果您只希望您的TableLayout 低于您的TextViews,那么在activity_home_page 中使用带有方向“vertical”的LinearLayout 而不是RelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_marginTop="10dp"
    android:text="welcome"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/display_user_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="33dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

并在循环末尾添加mainLinear.addView(tableLayout);

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

        for (int i = 0; i < 5; i++) {
            tableRow = new TableRow(getApplicationContext());
            for (int j = 0; j < 4; j++) {
                textView = new TextView(getApplicationContext());
                textView.setText("Hello");
                textView.setPadding(20, 20, 20, 20);
                tableRow.addView(textView);
            }
            tableLayout.addView(tableRow);
        }
        mainLinear.addView(tableLayout);

并删除setContentView(tableLayout);

希望这可行

【讨论】:

    【解决方案2】:

    将此添加到您的活动文件中:

    <TableLayout
         android:id="@+id/table1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/display_user_name"
        android:layout_marginTop="15dp" >
    
    </TableLayout>
    

    在你的课堂上:

    TableLayout tableLayout = (TableLayout)findViewById(R.id.table1);
             TableRow tableRow;
             TextView textView;
                for (int i = 0; i < no_days; i++) {
                    tableRow = new TableRow(getApplicationContext());
                    for (int j = 0; j < no_subjects; j++) {
                        textView = new TextView(getApplicationContext());
                        textView.setText("Hello");
                        textView.setPadding(20, 20, 20, 20);
                        tableRow.addView(textView);
                    }
                    tableLayout.addView(tableRow);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2014-05-13
      • 2015-06-02
      • 1970-01-01
      • 2011-05-31
      • 2021-12-22
      相关资源
      最近更新 更多