0

我正在努力实现一些我认为很容易但已经研究过负载并且仍在苦苦挣扎的事情。

我正在设置一个 ImageView 作为背景,其中部分图像是按钮应该位于顶部的区域。例如背景看起来有点像这样:

背景

这设置为 fitCenter 以便它在所有屏幕类型上以正确的纵横比进行缩放,如下所示:

背景调整大小

现在我正在尝试将 ImageButtons 放置在白色区域上,如下所示:

白色区域上的按钮

我正在尝试通过将每个 Imagebutton 对齐到背景 imageView 的左上角,然后设置边距以将其设置在正确的位置。这适用于某些屏幕类型,但是当背景因设备小于背景图像而被调整大小时,例如我发现顶部的按钮不会随之调整大小,所以我得到如下效果:

在此处输入图像描述

这是我正在尝试的布局类型(这里只有一个按钮):

<RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/outer" xmlns:tools="http://schemas.android.com/tools">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView1" android:src="@drawable/no_pills_pane" android:scaleType="fitCenter" android:adjustViewBounds="true" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> 
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageButton1" android:src="@drawable/pill_pink" android:scaleType="fitXY" android:background="@null" android:layout_marginTop="50dp" android:layout_marginLeft="44dp" android:layout_alignTop="@id/imageView1" android:layout_alignLeft="@id/imageView1"/>
</RelativeLayout>

我确信必须有一个更简单的方法来解决这个问题,任何帮助将不胜感激。

谢谢西蒙

4

1 回答 1

0

如果您只需要在 ImageView 中将 ImageButton 居中,只需将 alignBottom、alignLeft、alignRight、alignTop 设置为 ImageView

android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"
于 2012-07-04T22:53:57.060 回答