我正在制作一个自定义对话框,下部有两个按钮。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="@drawable/dialog_container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/questiontitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:text="@string/newquestion"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/writequestion"
android:layout_centerHorizontal="true"
android:layout_below="@+id/questiontitle"
android:text="@string/writequestion"
android:textSize="20sp"/>
<EditText
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/writequestion"
android:layout_marginTop="30dp"
android:hint="@string/questionhint" />
<TextView
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/question"
android:layout_marginTop="60dp"
android:id="@+id/centerview"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cancelbutton"
android:text="@string/cancel"
android:layout_below="@+id/question"
android:layout_toStartOf="@+id/centerview"
android:background="@drawable/buttonshape"
android:layout_marginTop="40dp"/>
<Button
android:id="@+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_toEndOf="@+id/centerview"
android:background="@drawable/buttonshape"
android:layout_alignParentRight="true"
android:text="@string/ok"
android:layout_marginTop="40dp"/>
</RelativeLayout>
在布局设计选项卡中,在 Android Studio 中,它显示完美,但在手机(三星 J7)中运行时,okbutton 正在填充父宽度...这是为什么?我希望按钮与 RelativeLayout 的底部对齐,一个在另一个的左侧。
谢谢。
最佳答案
你有一些错误的对齐方式,所以我为你修复了它们,一般来说,你确实以非常好的方式完成了大部分工作。
对于 future 的布局,我想推荐使用 constraintLayout ,它非常好,易于使用并且对不同的屏幕尺寸 react 灵敏。
这是新布局(我也在 galaxy j7 上检查过):
<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="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/questiontitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:text="newquestion"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/writequestion"
android:layout_centerHorizontal="true"
android:layout_below="@+id/questiontitle"
android:text="writequestion"
android:textSize="20sp"/>
<EditText
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/writequestion"
android:layout_marginTop="30dp"
android:hint="questionhint" />
<TextView
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/question"
android:layout_marginTop="60dp"
android:id="@+id/centerview"/>
<Button
android:id="@+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_marginTop="40dp"
android:layout_marginEnd="30dp"
android:layout_toEndOf="@+id/centerview"
android:text="cancel" />
<Button
android:id="@+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:text="ok"
android:layout_toStartOf="@+id/centerview"
android:layout_marginTop="40dp"/>
</RelativeLayout>
关于android - toEndOf 在自定义对话框布局中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54811289/