gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:12:09 26 4
gpt4 key购买 nike

我有一个带有 5 个 buttonsRelativeLayout :

<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>

有时在我的游戏中,我到了要保存它的地步。当它被保存时,应该在我的菜单中添加另一个按钮

     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,但我想把它放在上面,所有按钮都应该仍然居中。这可能吗?

最佳答案

希望这对您有所帮助。

lp.addRule(RelativeLayout.BELOW, R.id.below_id);

below_id 替换为您的特定 ID。

关于Android动态添加的按钮不适合布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com