Android动态添加按钮不适合布局

时间:2016-01-04 10:20:56

标签: android android-layout

我有一个RelativeLayout,其中包含5 buttons

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#212121"
tools:context="com.example.test.MainActivity"
android:id="@+id/menu"
android:gravity="center">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/main_menu"
    android:gravity="center">

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/singleplayer_text"
        android:id="@+id/singleplayer_button"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="initializeGame"
        android:clickable="true"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/multiplayer_button"
        android:layout_alignStart="@+id/multiplayer_button"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/multiplayer_text"
        android:id="@+id/multiplayer_button"
        android:layout_below="@+id/singleplayer_button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/deck_name"
        android:id="@+id/deck_button"
        android:layout_below="@+id/multiplayer_button"
        android:layout_alignLeft="@+id/multiplayer_button"
        android:layout_alignStart="@+id/multiplayer_button"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/options_text"
        android:id="@+id/options_button"
        android:layout_below="@+id/deck_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/exit_text"
        android:id="@+id/exit_button"
        android:layout_below="@+id/options_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />


</RelativeLayout>

在我的游戏中的某个时候,我来到了我想要保存它的位置。当它被保存时,我的菜单上应该添加另一个button

     resume= new Button(this);
     resume.setId((int) Math.random() * 10);
     resume.setText("Resume");
     resume.setTextColor(Color.parseColor("#212121"));
     resume.setBackgroundResource(R.drawable.button);
     int margin = (int) (8 * scale + 0.5f);
     RelativeLayout main_menu = (RelativeLayout)findViewById(R.id.main_menu);
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
     lp.setMargins(0, margin, 0,margin);
     main_menu.addView(resume, lp);

问题是,这个新的button resume涵盖了单人游戏button,但我希望它在它之上并且所有按钮仍应居中。 这可能吗?

2 个答案:

答案 0 :(得分:1)

  

添加要由RelativeLayout解释的布局规则。这个   方法只应用于不引用另一个的约束   兄弟(例如,CENTER_IN_PARENT)或取一个布尔值(TRUE为   true或0表示false)。要指定接受主题的动词,请使用   相反,addRule(int,int)。

根据您的要求 doesnt fit into layout ,您可以删除

删除此首先然后纠正您的addRule

阅读 RelativeLayout add rule

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

答案 1 :(得分:1)

希望这有帮助。

Enum

lp.addRule(RelativeLayout.BELOW, R.id.below_id); 替换为您的特定ID。

相关问题