表格行中的TextView不适合屏幕大小

时间:2016-05-26 10:42:54

标签: android

将文本视图添加到行,从而将行添加到表格布局。但是这一行会从屏幕上切下来。

  TextView point = new TextView(activity);
  TextView time = new TextView(activity);
  point.setTextSize(15);
  time.setTextSize(15);
  point.setPadding(5, 5, 5, 5);
  time.setPadding(5, 5, 5, 5);
  point.setText("smthing something")
  time.setText("smthing smthing")
  row.addView(point);
  row.addView(time);
  tableLayout.addView(row);

如何将textview包装在行中,以便行包裹在屏幕本身中。?

3 个答案:

答案 0 :(得分:1)

设置textView height wrap_content。

point.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

  TextView point = new TextView(activity);
  TextView time = new TextView(activity);
  LayoutParams layoutParams = new  TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  point.setLayoutParams(layoutParams);
  time.setLayoutParams(layoutParams);
  point.setTextSize(15);
  time.setTextSize(15);
  point.setPadding(5, 5, 5, 5);
  time.setPadding(5, 5, 5, 5);
  point.setText("smthing something")
  time.setText("smthing smthing")
  row.addView(point);
  row.addView(time);
  tableLayout.addView(row);

答案 2 :(得分:0)

感谢穆克什,这解决了我问题的大部分内容。 :)穿过屏幕的文字被包裹在屏幕内。 由于我的第一列包含长文本,第二列包含短文本。因此第一列必须适合多行,第二列必须符合单行。 (将textview设置为单行或多行没有帮助)

所以我通过将缩小列添加到我的'0th'并将列拉伸到我的'1st'来实现。这解决了我的整个问题,现在也不需要设置布局参数。

表格布局的XML:

<TableLayout
android:id="@+id/route_table"
android:layout_width="match_parent"
android:shrinkColumns="0"
android:padding="5dp"
android:stretchColumns="1">

<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/row1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:background="@drawable/table_row"
android:padding="10dp"
android:text="Stop: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time"
android:layout_gravity="right"
android:background="@drawable/table_row"
android:padding="10dp"
android:text="Time: "/>
</TableLayout>

`

相关问题