1

如何使文本textview仅填充宽度并开始新行以防文本溢出屏幕宽度?我正在尝试从 java 代码添加控件:

contentTable = (TableLayout) findViewById(R.id.contentTable);

            TableRow row = new TableRow(getApplicationContext());       
            TextView tv1 = new TextView(getApplicationContext());
            TextView tv2 = new TextView(getApplicationContext());

            tv1.setGravity(Gravity.RIGHT);
            tv2.setGravity(Gravity.RIGHT);

            row.setGravity(Gravity.RIGHT);


            tv1.setTextColor(Color.BLACK);
            tv2.setTextColor(Color.BLACK);


            tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
            tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));

            tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);


            tv1.setText(recipe.ingredients.get(i).name));
            tv2.setText(" "+recipe.ingredients.get(i).quantity);


            setTypeFace(tv1,tv2);

            row.addView(tv1);
            row.addView(tv2);

            contentTable.addView(row);
4

2 回答 2

0

layout_width="match_parent"或特定的 dp 宽度layout_height="wrap_content",我想就是这样。

于 2012-10-02T21:31:55.773 回答
0

TableLayoutandroid:shrinkColumns属性允许列出要缩小的列的索引(从零开始)。它将从列中删除不必要的额外空间(考虑到所有行)并缩小它。它使指定列的文本换行为多行。

<TableLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0,1" />
于 2014-01-07T15:36:57.763 回答