2015-02-07 48 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