Android: 动态添加 table 布局时出错
Android: Error in adding table layout dynamically
我正在尝试将 table 布局动态添加到 xml 文件,但 LayoutParams 导致错误。
Activity代码:
package com.pyrospiral.android.tabbedtimetable;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;
public class TimeTableWeek extends Activity {
TextView subname;
TextView timing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_table_week);
//Create text view
TextView text = new TextView(this);
text.setText("aaaaa");
//Layout params
LayoutParams params = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
//Find table in xml
TableLayout table = (TableLayout) findViewById(R.id.gridtable);
table.setLayoutParams(params);
//Create table row
TableRow row = new TableRow(this);
row.setLayoutParams(trparams);
row.setBackgroundColor(Color.GREEN);
row.addView(text);
}
}
XML 文件:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridtable">
</TableLayout>
</ScrollView>
错误:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.pyrospiral.android.tabbedtimetable, PID: 26691
java.lang.ClassCastException: android.widget.TableLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
将 TableLayout.LayoutParams 更改为 FrameLayout.LayoutParams 不会出现错误,但也不会显示任何内容。
您需要将 TableLayout 更改为 FrameLayout,如下所示:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
要查看您的文本,您必须将行添加到 table
table.addView(row);
Table 的布局参数已经在您的 XML 中定义,所以我认为您的变量 "params" 在那里是不必要的。
我正在尝试将 table 布局动态添加到 xml 文件,但 LayoutParams 导致错误。
Activity代码:
package com.pyrospiral.android.tabbedtimetable;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;
public class TimeTableWeek extends Activity {
TextView subname;
TextView timing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_table_week);
//Create text view
TextView text = new TextView(this);
text.setText("aaaaa");
//Layout params
LayoutParams params = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
//Find table in xml
TableLayout table = (TableLayout) findViewById(R.id.gridtable);
table.setLayoutParams(params);
//Create table row
TableRow row = new TableRow(this);
row.setLayoutParams(trparams);
row.setBackgroundColor(Color.GREEN);
row.addView(text);
}
}
XML 文件:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridtable">
</TableLayout>
</ScrollView>
错误:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.pyrospiral.android.tabbedtimetable, PID: 26691
java.lang.ClassCastException: android.widget.TableLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
将 TableLayout.LayoutParams 更改为 FrameLayout.LayoutParams 不会出现错误,但也不会显示任何内容。
您需要将 TableLayout 更改为 FrameLayout,如下所示:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
要查看您的文本,您必须将行添加到 table
table.addView(row);
Table 的布局参数已经在您的 XML 中定义,所以我认为您的变量 "params" 在那里是不必要的。