gpt4 book ai didi

android - 正在添加 TableRow 但不显示

转载 作者:行者123 更新时间:2023-11-29 22:07:33 30 4
gpt4 key购买 nike

我有一个小应用程序,需要定期将表格行添加到表格布局中。根据 getChildCount() 方法,正在添加行但不显示。任何建议将不胜感激

    TableLayout tbl = (TableLayout)findViewById(R.id.mytbl);
TableRow row = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
TextView txtView = new TextView(this);


row.setId(1234);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER);
row.setBackgroundResource(android.R.color.darker_gray);


LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
ll.setId(1589);
ll.setLayoutParams(lp);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundResource(R.color.header_background);
ll.setGravity(Gravity.CENTER);

String msg = "Sample Text";

txtView.setId(657);
txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
txtView.setTextColor(R.color.a_color);
txtView.setBackgroundResource(R.drawable.border_a);
txtView.setGravity(Gravity.TOP | Gravity.LEFT);
txtView.setText(msg);


ll.addView(txtView);
row.addView(ll);
tbl.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));//maybe add this with layout params tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

我试过 tbl.RequestLayout() 和 tbl.Invalidate() 但没有帮助。表格被包裹在 ScrollView 中,这是我对问题出在哪里的最佳猜测。我已经对此进行了数小时的修补,因此朝正确的方向轻推肯定会有所帮助

    <ScrollView
android:id="@+id/scrVw_lst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TableLayout
android:id="@+id/mytbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">

最佳答案

您是否运行了在您的 TableLayout 中添加一行的代码?我模拟了在单击 Button 时添加一行,但出现了一个尴尬的 Divide by 0 异常。

无论如何,将封闭的 LinearlayoutLayoutParams 更改为 TableRow.LayoutParams(其父级是 TableRow ):

    TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
ll.setId(1589);
ll.setLayoutParams(lp)

此外,如果这不能解决问题,请尝试添加:

android:fillViewport="true"

到 xml 布局中的 ScrollView 标签。

关于android - 正在添加 TableRow 但不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461639/

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