我有一个奇怪的问题,我希望有人可以帮助我。
我试图让 3 个按钮出现在另一个下方,从屏幕中心开始,上边距为 10dp。所以第一个按钮在屏幕的中心,第二个按钮在那个按钮的下方(没有向上推屏幕按钮的中心),第三个按钮在第二个下方。
每当我向我的 RelativeLayout(填满整个屏幕)添加一个大的(大于屏幕大小)背景图像时,它会导致第二个和第三个图像回到屏幕顶部(它们应该是如果我从第一个按钮中删除了 centerInParent 属性)。第一个按钮仍然停留在屏幕中间。
我想知道是否有人以前遇到过这个问题,最好的解决方案是什么?使用较小的图像可以工作,但并不理想,因为问题再次出现在较小的屏幕尺寸上,这需要我缩小所有图像,并且有时仍会出现在某些屏幕尺寸上。
这是我的 XML 文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >
<Button
android:id="@+id/button1"
android:layout_centerInParent="true"
style="@style/Button" />
<Button
android:id="@+id/button2"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/button1"
style="@style/Button" />
<Button
android:id="@+id/button3"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/button2"
style="@style/Button" />
</RelativeLayout>
按钮样式很简单:
<style name="Button">
<item name="android:layout_width">200dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:gravity">center</item>
<item name="android:padding">0dp</item>
<item name="android:textStyle">bold</item>
</style>
由于我在谷歌搜索时找不到任何对此问题有答案的人,所以一定是我误解了。这似乎应该是一个普遍的问题。
我也尝试将按钮包装在 LinearLayout 中,但它没有达到预期的效果,因为 LinearLayout 越大(通过添加按钮)它会越高,我有点希望按钮从屏幕中间向下。基本上,它与没有RelativeLayout 并且只是将您的重力设置为LinearLayout 中的中心并且方向设置为垂直时达到了相同的结果。
非常感谢任何帮助,我现在有点迷失了!
干杯!