gpt4 book ai didi

android - 以编程方式添加的 tableRow 未显示在 TableLayout 中

转载 作者:太空狗 更新时间:2023-10-29 13:32:11 25 4
gpt4 key购买 nike

我已经在 xml 中创建了一个 TableLayout。现在我想在我的 Activity 的 onCreate() 中以编程方式添加一个表行。方法 fillTable 应该可以完成这项工作,但 TableRow 没有显示在表格中。

这是我的代码:

在 Activity 中:

    public void fillTable() {
t1 = (TableLayout) findViewById(R.id.tabelle);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));

TextView rang = new TextView(this);
TextView mannschaftsname = new TextView(this);
TextView spiele = new TextView(this);
TextView siege = new TextView(this);
TextView unentschieden = new TextView(this);
TextView niederlagen = new TextView(this);
TextView tore = new TextView(this);
TextView doppelpunkt = new TextView(this);
TextView gegentore = new TextView(this);
TextView differenz = new TextView(this);
TextView punkte = new TextView(this);

rang.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
rang.setGravity(Gravity.CENTER_HORIZONTAL);
rang.setPadding(1, 1, 1, 1);
rang.setText("0");
rang.setTextColor(R.color.white);

mannschaftsname.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
mannschaftsname.setGravity(Gravity.CENTER_HORIZONTAL);
mannschaftsname.setPadding(1, 1, 1, 1);
mannschaftsname.setText("TEST");
mannschaftsname.setTextColor(R.color.white);

spiele.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
spiele.setGravity(Gravity.CENTER_HORIZONTAL);
spiele.setPadding(1, 1, 1, 1);
spiele.setText("0");
spiele.setTextColor(R.color.white);

siege.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
siege.setGravity(Gravity.CENTER_HORIZONTAL);
siege.setPadding(1, 1, 1, 1);
siege.setText("0");
siege.setTextColor(R.color.white);

unentschieden.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
unentschieden.setGravity(Gravity.CENTER_HORIZONTAL);
unentschieden.setPadding(1, 1, 1, 1);
unentschieden.setText("0");
unentschieden.setTextColor(R.color.white);

niederlagen.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
niederlagen.setGravity(Gravity.CENTER_HORIZONTAL);
niederlagen.setPadding(1, 1, 1, 1);
niederlagen.setText("0");
niederlagen.setTextColor(R.color.white);

tore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tore.setGravity(Gravity.CENTER_HORIZONTAL);
tore.setPadding(1, 1, 1, 1);
tore.setText("0");
tore.setTextColor(R.color.white);

doppelpunkt.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
doppelpunkt.setGravity(Gravity.CENTER_HORIZONTAL);
doppelpunkt.setPadding(1, 1, 1, 1);
doppelpunkt.setText(":");
doppelpunkt.setTextColor(R.color.white);

gegentore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
gegentore.setGravity(Gravity.CENTER_HORIZONTAL);
gegentore.setPadding(1, 1, 1, 1);
gegentore.setText("0");
gegentore.setTextColor(R.color.white);

differenz.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
differenz.setGravity(Gravity.CENTER_HORIZONTAL);
differenz.setPadding(1, 1, 1, 1);
differenz.setText("0");
differenz.setTextColor(R.color.white);

punkte.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
punkte.setGravity(Gravity.CENTER_HORIZONTAL);
punkte.setPadding(1, 1, 1, 1);
punkte.setText("0");
punkte.setTextColor(R.color.white);

tr.addView(rang);
tr.addView(mannschaftsname);
tr.addView(spiele);
tr.addView(siege);
tr.addView(unentschieden);
tr.addView(niederlagen);
tr.addView(tore);
tr.addView(doppelpunkt);
tr.addView(gegentore);
tr.addView(differenz);
tr.addView(punkte);

t1.addView(tr, new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}

显示表.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabelle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:stretchColumns="1" >

<TableRow
android:id="@+id/tablle_tableRow1"
android:gravity="center_horizontal" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_span="9"
android:text="Liganame"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
</TableRow>

<TableRow
android:id="@+id/tabelle_tableRow2"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="1dp" >

<TextView
android:id="@+id/textView_rang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_mannschaft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_spiele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="Sp."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_siege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="S"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_unentschieden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="U"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_niederlagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="1dp"
android:text="N"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_tore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_span="3"
android:gravity="right"
android:padding="1dp"
android:text="Tore"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_differenz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="Diff."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />

<TextView
android:id="@+id/textView_punkte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="Pkt."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
</TableRow>

<TableRow
android:id="@+id/tabelle_tableRow3"
android:gravity="center_horizontal" >

<TextView
android:id="@+id/rang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="1dp"
android:text="0"
android:textColor="@color/white" />

<TextView
android:id="@+id/mannschaft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="1dp"
android:text="BBBBBBBBBB"
android:textColor="@color/white" />

<TextView
android:id="@+id/spiele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />

<TextView
android:id="@+id/siege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />

<TextView
android:id="@+id/unentschieden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />

<TextView
android:id="@+id/niederlagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />

<TextView
android:id="@+id/tore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />

<TextView
android:id="@+id/doppelpunkt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text=":"
android:textColor="@color/white" />

<TextView
android:id="@+id/gegentore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />

<TextView
android:id="@+id/differenz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />

<TextView
android:id="@+id/punkte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />
</TableRow>

最佳答案

    rang.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
mannschaftsname.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
spiele.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
siege.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
unentschieden.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
niederlagen.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
doppelpunkt.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
gegentore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
differenz.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
punkte.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));

删除上面提到的代码行,我在这样做时也遇到了问题,然后我删除了这些行和动态显示的表格......

这应该有效。

关于android - 以编程方式添加的 tableRow 未显示在 TableLayout 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14344124/

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