11

我在设置表格行的布局参数(包含文本视图)时遇到了一些困难。

我想添加一些列以获得良好的布局。我正在动态地做它。(在代码中)

<TableRow> 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bye"/>
</TableRow>

我希望这两个文本视图成为两列并相应地在屏幕上布局。

4

1 回答 1

17

The thing you have already written should actually already create two columns, the thing is that they might not situate as you expect on the screen - the columns will be as narrow as possible. TableLayout tag in the Android layout has several attributes. One of them is the stretch columns - if given the described columns will be stretched so that to fill all the designated width. If you need all of them stretched evenly use star, if you want any specific column to be stretched covering the remaining space use its 1 based index (you can specify a group of indices). See here:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0,1" >
   <TableRow android:layout_width="fill_parent"> 
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="ok"    
      />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="bye" />
   </TableRow>
</TableLayout>

By the way if you need just a single row you might be able to do the same with LinearLayout and orientation="horizontal". If you have several rows, keep in mind that you are really dealing with table - all rows of a column will be situated exactly one above the other and the widest row will determine the width of the column.

于 2012-01-18T08:22:27.290 回答