2012-02-17 20 views
2

我有一張表格,它覆蓋整個屏幕幷包含可變數量的行。在表格內垂直對齊表格以覆蓋整個區域

我想知道我如何分配這些行垂直統一,以便他們佔用整個表的空間。

我試過layout_weight和weightSum,但它不工作。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout01" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="0,1,2" 
     android:id="@+id/maintable" 
     android:weightSum="3" > 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:layout_weight="1" > 
      <TextView 
       android:text="text 1_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

      <TextView 
       android:text="text 2_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

      <TextView 
       android:text="text 3_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:layout_weight="1" > 
      <TextView 
       android:text="text 1_2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

      <TextView 
       android:text="text 2_2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

      <TextView 
       android:text="text 3_2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:layout_weight="1" > 
      <TextView 
       android:text="text 1_3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

      <TextView 
       android:text="text 2_3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 

      <TextView 
       android:text="text 3_3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       /> 
     </TableRow> 

    </TableLayout> 
</LinearLayout> 

回答

2

你的代碼看起來錯了,因爲你不設置 '補父' 約束的垂直軸

<TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_weight="1" > 

應該

<TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 
0

請檢查下面的代碼。這將覆蓋表中的行均勻

grid_layout.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tableLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

</TableLayout> 

grid_layout填充有在代碼文本視圖如下

public class GridLayoutActivity extends Activity { 
    private static final int NO_OF_ROWS = 5; 
    private static final int NO_OF_COLUMS = 5; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.grid_layout); 
     TableLayout gridview = (TableLayout) findViewById(R.id.tableLayout); 
     for (int rowCnt = 0 ; rowCnt < NO_OF_ROWS; rowCnt++) { 
      TableRow row = new TableRow(this); 
     TableLayout.LayoutParams tableLayoutLP = new TableLayout.LayoutParams(); 
     tableLayoutLP.weight = 1; 
     row.setLayoutParams(tableLayoutLP); 

     for (int column = 0; column < NO_OF_COLUMS; column++) { 
     TextView tv = new TextView(this); 
     TableRow.LayoutParams tableRowLP = new TableRow.LayoutParams(); 
     tableRowLP.setMargins(2, 2, 2, 2); 
     tableRowLP.weight=1; 
     tableRowLP.height = TableRow.LayoutParams.MATCH_PARENT; 
     tv.setTextColor(Color.WHITE); 
     tv.setGravity(Gravity.CENTER); 
     tv.setBackgroundColor(Color.DKGRAY); 
     tv.setLayoutParams(tableRowLP); 

     tv.setText("YourText");//Add your text here 
     row.addView(tv); 
     } 

     gridview.addView(row); 
    } 
} 

}

整個屏幕