【发布时间】:2023-03-05 02:59:01
【问题描述】:
我的 Android 应用中有一个相当简单的 Activity,它显示三个按钮,每个按钮启动一个不同的 Activity。目前,我使用 RelativeLayout 将中间按钮水平和垂直居中,然后将顶部和底部按钮放置在距离中间按钮 30dp 的位置(并且水平居中)。
不过,我想做的是让按钮拉伸到屏幕宽度的一定百分比。我不知道如何做到这一点并保持按钮居中。是否有一个好的对象可以用作按钮两侧的 LinearLayout 中的“填充物”(所以我可以设置权重)?或者有没有不涉及 LinearLayout 的方法?
当前布局的 XML 是:
<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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:onClick="button1Callback"
android:text="@string/button1Label" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="button2Callback"
android:text="@string/button2Label" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/button3Label" />
</RelativeLayout>
【问题讨论】:
-
你能以某种方式展示你想要它的样子吗?油漆、gimp 等。我想我理解了,但我可能错了。
-
你如何根据屏幕百分比修改它们的宽度?看看这个,如果您将宽度更改为 100dp,它们应该都保持居中。