TableRow span不适用于动态添加的行

Kam*_*men 5 android tablelayout tablerow

我有以下问题跨越动态添加行到滚动视图内的TableLayout.行遵循以下模式:

第1行:整个表格上的单元格
第2行:两个单元格
第3行:整个表格上的单元格
...
行N:两个单元格

问题是跨越行的一个单元格的行实际上根本不跨越.它到达屏幕中间的某个点,只是在高处包裹.

以下是Android 2.3.3下的问题的屏幕截图: 跨度布局问题

请注意,下面的示例过于简单(但仍然跨越不起作用).他们准备尝试 - 只需创建文件.我调试了,似乎布局参数在某种程度上消失了.此外,如果TableLayout在main.xml文件中是硬编码的,那么没有问题.不幸的是,我需要动态生成视图.

TestProject.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class TestProject extends Activity {
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                TableLayout table = new TableLayout(this);
                ScrollView contentHolder = (ScrollView) findViewById(R.id.scrollView1);
                contentHolder.addView(table, new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
                TableRow row1 = (TableRow) View.inflate(this, R.layout.table_span_row, null);
                TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
                        TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
                rowSpanLayout.span = 2;
                table.addView(row1, rowSpanLayout);
                TableRow row2 = (TableRow) View.inflate(this, R.layout.table_row_columns, null);
                table.addView(row2);
                    }
    }
Run Code Online (Sandbox Code Playgroud)

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<ScrollView android:id="@+id/scrollView1" android:layout_height="fill_parent"
    android:layout_width="fill_parent">
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

table_row_columns.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content" android:text="This is column one" android:layout_weight="1" android:layout_width="fill_parent"></TextView>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Column 2"></TextView>
</TableRow>
Run Code Online (Sandbox Code Playgroud)

table_span_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content" android:layout_width="fill_parent">
    <LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_span="2" android:background="#FF333333">
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="This should span on the whole row"></TextView>
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="... but it does not do so"></TextView>
    </LinearLayout>
</TableRow>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过requestLayout并且无效但无济于事.

PS我也欢迎有关如何在ScrollView中动态替换视图的建议

Kam*_*men 2

好的,我找到了解决方案。只需删除我想要跨表的行的 TableRow 标记即可。

表_span_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:background="#FF333333">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_weight="1" android:text="This should span on the whole row" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="... and now it does" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我不知道如果我有 3 列并且想要跨越其中两列上的一个单元格,会发生什么:)