Arrangement of buttons
I have created an array of buttons but all the buttons are arranged in vertical order.
I want 3 buttons in one row, next 3 buttons in second row and so on.Here is my code please check it where it should be done.
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);
}
}
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**
}
}
Regards, Stéphane
Copy, paste, enjoy..
<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"/>
and read this:Android Layout Tricks #1
精彩评论