Диапазон TableRow не работает для динамически добавленных строк
У меня есть следующая проблема, заключающая динамически добавленные строки в TableLayout внутри прокрутки. Строки следуют этому шаблону:
Строка 1: ячейка, развернутая по всей таблице
Строка 2: две ячейки
Строка 3: ячейка, развернутая по всей таблице
...
Строка N: две ячейки
Проблема заключается в том, что строка с одной ячейкой, охватывающей всю строку, фактически не распространяется вообще. Он достигает в какой-то момент в середине экрана и просто обертывается на высоте.
Вот скриншот проблемы под Android 2.3.3:
![Span layout problem]()
Обратите внимание, что приведенные ниже примеры упрощены (но все еще не работает). Они готовы попробовать - просто создайте файлы. Я отлаживался, и кажется, что параметры макета каким-то образом исчезают на этом пути. Кроме того, если 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);
}
}
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>
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 и сделал недействительным, но безрезультатно.
P.S. Я также приветствую советы о том, как динамически заменить представления в ScrollView
Ответы
Ответ 1
ОК, я нашел решение. Это просто было оставить тег 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 столбца, и я хотел бы охватить одну из ячеек на двух из них:)
Ответ 2
Может быть, это поможет кому-то. Даже этот вопрос был более двух лет, и я вижу ту же ошибку прямо сейчас. То есть rowSpanLayout.span = 2; не работает. Но в xml layout_span он работает.
Я нашел работоспособное решение:
1. создайте вложение файла 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>
- Получить свойства Layout из этого элемента макета:
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);
Это работает.