gpt4 book ai didi

android - 如何将新行动态添加到表格布局

转载 作者:行者123 更新时间:2023-11-29 01:48:11 26 4
gpt4 key购买 nike

在我的 android 应用程序中,我有一个表格布局。我想动态插入行。但问题是行是水平添加的。我希望每个表格行都垂直堆叠。到目前为止我有这个:

    TableLayout mytable = (TableLayout)findViewById(R.id.studentContainer);

TableRow tablerow = new TableRow(this);
tablerow.setOrientation(TableRow.VERTICAL);
EditText g = new EditText(this);
g.setText("AAAAAAAAA");
g.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText g2 = new EditText(this);
g2.setText("BBBBBBBBBB");
g2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tablerow.addView(g);
tablerow.addView(g2);
mytable.addView(tablerow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

TableRow tablerow2 = new TableRow(this);
tablerow2.setOrientation(TableRow.VERTICAL);
EditText g4 = new EditText(this);
g4.setText("AAAAAAAAA");
g4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText g5 = new EditText(this);
g5.setText("BBBBBBBBBB");
g5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tablerow.addView(g4);
tablerow.addView(g5);
mytable.addView(tablerow2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

XML

            <TableLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/studentContainer" >
</TableLayout >

有谁知道这里出了什么问题吗?

谢谢

最佳答案

尝试将第一行放在 xml 中并旋转整个表格,必要时编辑 text_views。像这样

XML

<TableLayout
android:id="@+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:rotation="-90"
android:stretchColumns="0,1,2,3" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column1"
android:textColor="#000000"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column2"
android:textColor="#000000"/>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column3"
android:textColor="#000000"/>

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column4"
android:textColor="#000000"/>

</TableRow>
</TableLayout>

然后像这样编码

TableRow tr = new TableRow(this);
tr.setPadding(0,20,0,20);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
TextView tv_item4 = new TextView(this);

tv_item1.setText(M_Number);
tv_item1.setGravity(Gravity.CENTER);


tv_item2.setText(M_Number);
tv_item2.setGravity(Gravity.CENTER);

tv_item3.setText(M_Number);
tv_item3.setGravity(Gravity.CENTER);

tv_item4.setText(M_Number);
tv_item4.setGravity(Gravity.CENTER);

tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
tr.addView(tv_item4);

Table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

关于android - 如何将新行动态添加到表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20088139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com