简体   繁体   English

TableLayout中的TextView数组

[英]TextView arrays in TableLayout

I am wanting to do 4 columns with dynamically changing rows of data. 我想通过动态更改数据行来做4列。 I would do this with LinearLayout, but I cannot find a good way to get a 4x10 set of textviews. 我可以使用LinearLayout做到这一点,但是我找不到找到4x10文本视图集的好方法。 This .java is trowing a fatal error which is listed at the bottom: 该.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++;
}
}

I have a (basically) blank .XML to work with: 我有一个(基本上)空白的.XML要使用:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/debtlist"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</TableLayout>

my LogCat: 我的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)

If anyone has any more efficient ways to get a 4X1-10 set of TextViews to be displayed based on the amount of data input by the user, please let me know. 如果有人根据用户输入的数据量有任何更有效的方法来获取要显示的4X1-10 TextView集,请告诉我。

Also I'm using API level 12 我也在使用API​​级别12

as in log : 如在日志中:

ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams ClassCastException:无法将android.app.ActionBar $ LayoutParams强制转换为android.widget.LinearLayout $ LayoutParams

means you are importing ActionBar.LayoutParams for setting LayoutParams to TableLayout and Buttons so use LinearLayout.LayoutParams as: 意味着您要导入ActionBar.LayoutParams以将LayoutParams设置为TableLayout和Button,因此将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);

    }

You are getting class cast exception because you are using Table Layout and inflating context of ActionBar Linear Layout. 由于使用表布局和ActionBar线性布局的膨胀上下文,因此您将获得类强制转换异常。

java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.app.ActionBar $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams

is there a way 4 columns per row for x number of rows ? 有没有办法为x的行数每行4列?

Use a custom layout for this and use it . 为此使用自定义布局并使用它。

 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);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 textView调整为tableLayout中最长的TextView - textView resizes to longest TextView in the tableLayout TableLayout-内联TextView和ImageView - TableLayout - TextView and ImageView inline LinearLayout-在TextView之后添加TableLayout - LinearLayout - adding a TableLayout after a TextView TableLayout中TextView的动态跨度调整 - Dynamic Span Adjustment for TextView In TableLayout 即使使用ArrayList中的值,带有TextView的Dynamic TableLayout也为空 - Dynamic TableLayout with TextView is empty even with values in ArrayList 将TableLayout中的TextView滚动到屏幕的可见区域 - Scroll a TextView in TableLayout into the visible area of screen 更改Android中TableLayout中TextView和Column的文字大小 - Change the text size of a TextView and a Column in TableLayout in Android android自动横向滚动在TableLayout的TableRow中的TextView? - android automatic horizontal scrolling TextView in a TableRow in a TableLayout? 以编程方式将TableRows插入TableLayout-TableRow中的TextView将不会显示 - Inserting TableRows into TableLayout programmatically - TextView inside TableRow won't display 如何使用Android将动态TableRow放置在TableLayout中并将Textview动态放置在TableRow中? - how to put dynamic TableRow in TableLayout and place Textview dynamically in TableRow using Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM