java - Android TableLayout 内容不显示

标签 java android dynamic tablelayout

我有一个动态填充的 TextViews 工作列表。但是现在我想把它做成一个多列的TableView。我已经动态创建行并将它们添加到布局中,但不会显示任何内容。谁能指出我看不到的地方?

调试器显示包含包含文本的 TextView 的行的布局。我已尝试使内容匹配父级的宽度和高度,但仍然无法显示任何内容。

这是代码。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detailScroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/detailLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/almost_black"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/detailTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/almost_black"
        android:orientation="vertical"
        android:stretchColumns="1" >

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

            <TextView
                android:id="@+id/testText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Testing" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/protocolButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="40sp"
        android:layout_marginRight="40sp"
        android:background="@drawable/protocol_button"
        android:minHeight="40sp"
        android:onClick="getProtocol"
        android:padding="5sp"
        android:text="@string/protocol_button_text" >
    </Button>
</LinearLayout>
</ScrollView>

Java

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
layout = (TableLayout) this.findViewById(R.id.detailTable);
    TableRow tr;

    if (project.ProtocolNumber != null)
    {
        tr = BuildTableRow(res.getString(R.string.protocol_number_header), project.ProtocolNumber);
        layout.addView(tr);
    }
private TextView BuildTextView(String string)
{
    TextView tv = new TextView(this);
    tv.setLayoutParams(lparams);
    tv.setText(Html.fromHtml(string));
    tv.setTextColor(Color.BLACK);
    tv.setPadding(5, 5, 5, 5);

    return tv;
}// BuiltTextView

private TableRow BuildTableRow(String left, String right)
{
    TableRow tr = new TableRow(this);
    LayoutParams rowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    tr.setLayoutParams(rowParams);
    tr.setBackgroundColor(Color.RED);
    tr.addView(BuildTextView(left));
    tr.addView(BuildTextView(right));
    return tr;
}

谢谢。

最佳答案

诀窍是摆脱 LayoutParams

关于java - Android TableLayout 内容不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17353974/

相关文章:

java - 出界 4>=4

java - 通过键盘输入在 Canvas 上绘制文本

android - 不显示子菜单后的可选菜单项

android - 如何引用资源值中的 ?attr/actionBarSize?

c# - 使用 C# 进行动态 SQL Server 访问的建议

Javascript - 更改 Google Chart onClick

java - Smack 客户端 - 尽管连接中止,用户仍然是 'online'

java - Eclipse插件热部署

java - 为 Zebra 设备开发 Android 应用程序

c# - 将动态对象映射到接口(interface)并向 IoC 注册