按钮的安排

时间:2011-06-20 10:29:08

标签: android

我创建了一系列按钮,但所有按钮都按垂直顺序排列 我想在一行中有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);

    }
}

2 个答案:

答案 0 :(得分:0)

public void onCreate(Bundle 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**
}

}

此致  斯特凡

答案 1 :(得分: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