android中textview中的文本溢出

时间:2012-10-02 21:28:10

标签: android textview overflow

如果文本溢出屏幕宽度,如何使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);

2 个答案:

答案 0 :(得分:0)

我认为

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

答案 1 :(得分:0)

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

<TableLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0,1" />