我在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);
}
如何以编程方式翻译此命令android:layout_below="@id/example1"?
使用RelativeLayout。布局参数。使用addRule()
,例如将其与RelativeLayout一起使用。BELL和id(R.id.example1)