java - 通过Java动态设置表格布局

标签 java android dynamic android-tablelayout

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

这段代码位于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 布局中定义的 TextView 。

这是我的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>

最佳答案

如果您只想将 TableLayout 置于 TextViews 下方,则使用方向为“vertical”的 LinearLayout activity_home_page 而不是 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);

希望这有效

关于java - 通过Java动态设置表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24110231/

相关文章:

java.lang.NoSuchMethodError : main error in simple java program

java - 显示链表的第一个节点

android - 每次生成GCM注册ID

android - 如何在 React Native 中构建调试 apk

android - 应用 Colorfilter 后滚动时 Ninepatch 缩放错误?

python - Pandas 中具有特定总长度的动态索引

go - go中动态调用模型

java - 通过 REST Controller 使用 Spring Data JPA 和 QueryDsl 的异常

Java FX : Detected [iscc. exe] 版本 0,但需要版本 5。由于配置问题跳过了 Bundler EXE 安装程序

dictionary - 带有嵌套 map 的 terraform 嵌套动态 block