i have in xml tis code:
<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开发者_StackOverflow: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>
I want to create these buttons programmatically and i use:
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);
}
How can I translate this command android:layout_below="@id/examp1" programmatically?
Use RelativeLayout.LayoutParams. Use addRule()
, for your example use it with RelativeLayout.BELOW and the id (R.id.examp1)
精彩评论