Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java动态设置表格布局_Java_Android_Dynamic_Android Tablelayout - Fatal编程技术网

用Java动态设置表格布局

用Java动态设置表格布局,java,android,dynamic,android-tablelayout,Java,Android,Dynamic,Android Tablelayout,我需要我的屏幕在活动布局中定义两个文本视图。在这些视图下面,我想设置一个表,其行数和列数取决于用户的输入(动态) 此代码位于活动的oncreate中 super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_page); TableLayout tableLayout = new TableLayout(getApplicationContext()); TableRow

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

此代码位于活动的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);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u主页);
TableLayout TableLayout=新的TableLayout(getApplicationContext());
TableRow TableRow;
文本视图文本视图;
对于(int i=0;i
但是这段代码占据了整个屏幕,我无法拥有我在活动布局中定义的那些文本视图

这是我的活动主页.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
     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);
            }
TableLayout TableLayout=(TableLayout)findviewbyd(R.id.table1);
TableRow TableRow;
文本视图文本视图;
对于(int i=0;i
如果您只想将
表格布局
放在
文本视图的下方
,请在
活动主页
中使用
线性布局
,而不是
相对布局

<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>
并删除
setContentView(tableLayout)


希望这能起作用

发布您的活动\u主页\u页面Yes activity\u home\u page.xml
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);