2015-02-07 42 views
0

我想知道如何用矩陣結構創建佈局。例如: :第一行有5個按鈕,第二個原始有5個按鈕,依此類推。Eclipse android佈局矩陣結構

我試圖做到這一點沒有任何真正的運氣。 我的編程平臺就是Eclipse,Java和Android的

+1

TableLayout與TableRows是去 – 2015-02-07 03:16:41

+0

我可以做手工,在XML編碼的好方法。像relativelayout等 – zeev1079 2015-02-07 03:34:37

+0

嘗試GridView。這很容易,並會做你想要的。 – 2015-02-07 03:51:15

回答

0

嘗試TableLayout。 :這種佈局將其子項排列成行和列。

下面是創建在第一排3個按鈕和第二排3個按鈕一個非常基本的TableLayout。

可以通過添加2更多的TableRow對象置於TableLayout延伸這有每行中的5個按鈕。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 



    <TableRow 
          android:id="@+id/tableRow1" 
          android:layout_height="wrap_content" 
          android:layout_width="match_parent"> 
          <Button 
           android:id="@+id/TextView04" android:text="Row 1 Button 1" 
           android:layout_weight="1" android:background="#dcdcdc" 
           android:textColor="#000000" 
           android:padding="20dip" android:gravity="center"/> 
          <Button 
           android:id="@+id/TextView04" android:text="Row 1 Button 2" 
           android:layout_weight="1" android:background="#d3d3d3" 
           android:textColor="#000000" 
           android:padding="20dip" android:gravity="center"/> 
          <Button 
           android:id="@+id/TextView04" android:text="Row 1 Button 3" 
           android:layout_weight="1" android:background="#cac9c9" 
           android:textColor="#000000" 
           android:padding="20dip" android:gravity="center"/> 


         </TableRow> 


    <TableRow 
          android:id="@+id/tableRow1" 
          android:layout_height="wrap_content" 
          android:layout_width="match_parent"> 
          <Button 
           android:id="@+id/TextView04" android:text="Row 2 Button 1" 
           android:layout_weight="1" android:background="#dcdcdc" 
           android:textColor="#000000" 
           android:padding="20dip" android:gravity="center"/> 
          <Button 
           android:id="@+id/TextView04" android:text="Row 2 Button 2" 
           android:layout_weight="1" android:background="#d3d3d3" 
           android:textColor="#000000" 
           android:padding="20dip" android:gravity="center"/> 
          <Button 
           android:id="@+id/TextView04" android:text="Row 2 Button 3" 
           android:layout_weight="1" android:background="#cac9c9" 
           android:textColor="#000000" 
           android:padding="20dip" android:gravity="center"/> 


         </TableRow> 

</TableLayout> 

請注意,TableLayout容器不顯示其行,列或單元格的邊框線。

如果您想添加幾行動態使用Java代碼的情況下,下面是例子:

TableLayout tl = (TableLayout) findViewById(R.id.yourtablelayout); 
/* Create a new row to be added. */ 
TableRow tr = new TableRow(this); 
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
/* Create a Button to be the row-content. */ 
Button b = new Button(this); 
b.setText("add your button text here "); 
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
/* Add Button to row. */ 
tr.addView(b); 
/* Add row to TableLayout. */ 
//tr.setBackgroundResource(R.drawable.sf_gradient_03); 
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 
+0

謝謝,我真的很感謝你的幫助和時間。 – zeev1079 2015-02-07 16:38:49

+0

當然!您可能必須根據您的要求在上述佈局中自定義佈局以添加美容效果! – AADProgramming 2015-02-07 16:40:53