2016-05-26 41 views
1

将文本视图添加到行,从而将行添加到表格布局。但该行从屏幕上切下。表格行中的TextView不适合屏幕尺寸

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,使行被包裹在屏幕本身..?

回答

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

设置你的textView高度wrap_content。

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

感谢穆克什,即解决了大多数我的问题的一部分。 :) – lancer025

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> 

`