按钮下面的其他按钮编程android

按钮下面的其他按钮编程android,android,button,position,Android,Button,Position,我有xml tis代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button

我有xml tis代码:

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/examp1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/examp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/examp1" />
</RelativeLayout>

我希望以编程方式创建这些按钮,并使用:

RelativeLayout mainLayout;
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);

for (int i=0; i<1; i++)
{
     Button new_button = new Button(this);
     new_button.setText("blabla");
     new_button.setId(i);
     new_button.setLayoutParams(new LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,         
                        ViewGroup.LayoutParams.WRAP_CONTENT));

     mainLayout.addView(new_button);
}
RelativeLayout主布局;
mainLayout=(RelativeLayout)findViewById(R.id.mainLayout);

对于(int i=0;iUse.Use
addRule()
),例如,将其与RelativeLayout.down和id(R.id.examp1)一起使用

我的意思是,当你问一个问题,当有人回答你的问题时,如果这个问题适合你的问题,你必须接受这个答案。这样用户才会有兴趣回答你的问题。Thnaks Andro..我会听从你的建议。所以,我必须将每个按钮放在单独的布局上,然后使用addRule,然后添加每个按钮n布局?您想做什么?如果您想将按钮一个一个地布置在另一个按钮下面,则使用垂直方向的线性布局。如果您仍然想使用RelativeLayout进行此操作,则需要在相同的RelativeLayout上使用addRule()方法,无需将每个按钮放置在单独的布局上。