5

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

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

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

这是Android 2.3.3下的问题截图: 跨度布局问题

请注意,下面的示例过于简单(但仍然无法跨越)。他们已准备好尝试 - 只需创建文件。我进行了调试,似乎布局参数在此过程中不知何故消失了。此外,如果 TableLayout 是硬编码在 main.xml 文件中,则没有问题。不幸的是,我需要动态生成视图。

测试项目.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);
                    }
    }

主要的.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>

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>

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>

我试过 requestLayout 和 invalidate 但无济于事。

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

4

2 回答 2

2

好的,我找到了解决方案。只是为我想要跨越表格的行删除 TableRow 标记。

table_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>

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

于 2011-05-27T08:01:44.977 回答
1

可能会对某人有所帮助。即使这个问题已经超过 2 年了,我现在也看到了同样的错误。即rowSpanLayout.span = 2; 不起作用。但在 xml layout_span 中它可以工作。我找到了可行的解决方案:1.使用布局属性创建layour xml文件注入:

 <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
     <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
          <TextView
                android:id="@+id/check1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_span="3"
                android:text="TextView" />
     </TableRow>
     </TableLayout>
  1. 从此布局元素获取布局属性:
TableRow.LayoutParams blp;
blp = (TableRow.LayoutParams)check1.getLayoutParams();
blp.width = 100;
blp.height = 50;
TextView tw = new TextView(this);
tw.setTextSize(tsEL);
tw.setLayoutParams(blp);
tw.setText(R.string.empty);
tr.addView(tw);

有用。

于 2013-09-08T09:37:00.977 回答