2011-06-20 73 views
1

我已经创建了一个按钮阵列,但所有按钮都按垂直顺序排列。
我想要3个按钮在一行中,接下来3个按钮在第二行中,依此类推。按钮的排列

这是我的代码,请检查它应该在哪里完成。

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    LinearLayout layout = (LinearLayout)findViewById(R.id.liLayout); 
    for (int i = 1; i < 10; i++) 
    { 

     LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(

       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT 
     ); 
     Button b = new Button(this); 
     b.setText("" + i); 
     b.setId(100 + i); 
     b.setWidth(50); 
     b.setHeight(20); 
     layout.addView(b, p); 

    } 
} 

回答

0

公共无效的onCreate(捆绑savedInstanceState) {

super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
//should have a vertical orientation 
LinearLayout layout = (LinearLayout) findViewById(R.id.liLayout); 

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(

      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT 
    ); 

LinearLayout rowLayout = null; **<--null will cause an exception** 
*layout.addView(rowLayout)* **<--must be added to make sure the object/element exist/not null** 

for (int i = 1; i < 10; i++) 
{ 
    if((i % 3) == 1) 
    { 
      rowLayout = new LinearLayout(this); 
      layout.addView(rowLayout, p); 
    }//if 
    Button b = new Button(this); 
    b.setText(""+ i); 
    b.setId(100+i); 
    b.setWidth(50); 
    b.setHeight(20); 
    rowLayout.addView(b, p); **<-- you can't add to rowLayout at first, because it doesn't exist yet** 
} 

}

问候, 斯特凡

+0

喜stephane..the语句的RowLayout =新的LinearLayout();给出了编译错误。“构造函数LinearLayout没有定义”,当我给rowLayout =新的LinearLayout(null)它的工作..但给运行时错误 – AndroidDev

+0

好吧,我错过了上下文,答案已被纠正。 – Snicolas

+0

嘿stephane ..我应该做什么使按钮填充屏幕水平,(所有都是相同的大小),因为以上所有按钮我有文本框..所以我怎么可以整合所有这些,以便他们都填满屏幕水平 – AndroidDev

0

复制,粘贴,享受..

<RelativeLayout android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

<Button android:id="@+id/button1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="Button1"/> 

<Button android:id="@+id/button2" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_toRightOf="@+id/button1" 
     android:layout_alignTop="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:text="Button2"/> 

<Button android:id="@+id/button3" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_toRightOf="@+id/button2" android:layout_alignTop="@+id/button2" android:layout_alignBottom="@+id/button2" 
     android:text="Button3"/> 

<Button android:id="@+id/button4" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/button1" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_alignRight="@+id/button1" 
     android:text="Button4"/> 

<Button android:id="@+id/button5" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/button2" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_alignRight="@+id/button2" 
     android:text="Button5"/> 

<Button android:id="@+id/button6" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignLeft="@+id/button3" android:layout_alignRight="@+id/button3" 
     android:layout_below="@+id/button3" 
     android:text="Button6"/> 

<Button android:id="@+id/button7" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignLeft="@+id/button4" 
     android:layout_alignRight="@+id/button4" 
     android:layout_below="@+id/button4" 
     android:text="Button7"/> 

<Button android:id="@+id/button8" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/button5" 
     android:layout_alignLeft="@+id/button5" 
     android:layout_alignRight="@+id/button5" 
     android:text="Button8"/> 


<Button android:id="@+id/button9" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/button6" android:layout_alignLeft="@+id/button6" android:layout_alignRight="@+id/button6" 
     android:text="Button9"/> 

并读取此:Android Layout Tricks #1

+0

谢谢,但那件事我已经完成了.. – AndroidDev

+0

大声笑...你使用图形布局工具吗?它更帮助.. –

+0

假设我不得不创建100个按钮..不该我应该做什么......只是简单地添加100个这样的buutons ..思考如果我nedd 1000个按钮。 – AndroidDev