gpt4 book ai didi

java - 如何使用 Android 在 TableLayout 中放置动态 TableRow 并在 TableRow 中动态放置 Textview?

转载 作者:行者123 更新时间:2023-11-30 03:16:21 25 4
gpt4 key购买 nike

我是新手,很难达到我要求的输出。代码://这段代码在ScrollView里面

 <LinearLayout 
android:id="@+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"

></TableLayout>

</LinearLayout>

Java 代码:

setContentView(R.layout.status_view);
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
TableRow statusTableRow = new TableRow(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(TableRow.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);


for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout,rlp);

}
statusTableLayout.addView(statusTableRow);}

code output

请告诉我在给定图像所需的产品中,我需要对当前代码进行哪些更改 required outout

最佳答案

您可以像这样使用 LinearLayout 而不是 TableRow

TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
LinearLayout statusTableRow = new LinearLayout(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(LinearLayout.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);


for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);

RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout);

}
statusTableLayout.addView(statusTableRow);}

同样把所有的东西都放在Relativelayout里面,然后像这样放在ScrollView里面

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"

>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>

</TableLayout>

</LinearLayout>

</RelativeLayout>

</ScrollView>

关于java - 如何使用 Android 在 TableLayout 中放置动态 TableRow 并在 TableRow 中动态放置 Textview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042835/

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