TableLayout 中的 TextView 数组
Posted
技术标签:
【中文标题】TableLayout 中的 TextView 数组【英文标题】:TextView arrays in TableLayout 【发布时间】:2013-10-13 04:15:10 【问题描述】:我想做 4 列动态变化的数据行。我会用 LinearLayout 来做到这一点,但我找不到获得一组 4x10 文本视图的好方法。此 .java 引发了一个致命错误,该错误列在底部:
public class DebtList extends Activity
Integer trigger = 5;
Double totalDebt = 0.00;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
//Bundle extras = getIntent().getExtras();
String[] debtNames = new String[10];
Double[] debtAmount = new Double[10];
Double[] debtRate = new Double[10];
Double[] debtTerm = new Double[10];
Integer stopper = trigger+1;
TableLayout tl=(TableLayout)findViewById(R.id.debtlist);
TableRow tr1 = new TableRow(this);
TextView textview = new TextView(this);
for(int i=0; i<stopper; i++)
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr1.setId(i);
textview.setText("" + i);
tr1.addView(textview);
tl.addView(tr1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT));
tr1.setId(i);
trigger++;
我有一个(基本上)空白的 .XML 可以使用:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/debtlist"
android:layout_
android:layout_ >
</TableLayout>
我的 LogCat:
10-13 04:12:07.412: E/AndroidRuntime(799): FATAL EXCEPTION: main
10-13 04:12:07.412: E/AndroidRuntime(799): java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.TableLayout.measureVertical(TableLayout.java:468)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-13 04:12:07.412: E/AndroidRuntime(799): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.os.Looper.loop(Looper.java:132)
10-13 04:12:07.412: E/AndroidRuntime(799): at android.app.ActivityThread.main(ActivityThread.java:4025)
10-13 04:12:07.412: E/AndroidRuntime(799): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 04:12:07.412: E/AndroidRuntime(799): at java.lang.reflect.Method.invoke(Method.java:491)
10-13 04:12:07.412: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-13 04:12:07.412: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-13 04:12:07.412: E/AndroidRuntime(799): at dalvik.system.NativeStart.main(Native Method)
如果有人有任何更有效的方法来根据用户输入的数据量显示一组 4X1-10 的 TextView,请告诉我。
我也在使用 API 级别 12
【问题讨论】:
【参考方案1】:如日志:
ClassCastException: android.app.ActionBar$LayoutParams 不能被强制转换 到 android.widget.LinearLayout$LayoutParams
表示您正在导入 ActionBar.LayoutParams
以将 LayoutParams 设置为 TableLayout 和 Buttons,因此请使用 LinearLayout.LayoutParams 作为:
for(int i=0; i<stopper; i++)
tr1.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//.....
tr1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT)));
tr1.setId(i);
【讨论】:
仍然只有垂直或水平,这取决于我如何设置 LL。【参考方案2】:由于您正在使用表格布局并膨胀 ActionBar 线性布局的上下文,因此您会遇到类转换异常。
java.lang.ClassCastException:android.app.ActionBar$LayoutParams 无法转换为 android.widget.LinearLayout$LayoutParams
有没有办法每行 4 列 x 行?
为此使用自定义布局并使用它。
for(int i=0; i<stopper; i++)
// Inflate your row "template" and fill out the fields.
TableRow row = (TableRow)LayoutInflater.from(context).inflate(R.layout.attrib_row, null);
((TextView)row.findViewById(R.id.attrib_name)).setText(b.NAME);
((TextView)row.findViewById(R.id.attrib_value)).setText(b.VALUE);
table.addView(row);
【讨论】:
以上是关于TableLayout 中的 TextView 数组的主要内容,如果未能解决你的问题,请参考以下文章