固定位置的ImageView

3

在我的RelativeLayout中,我有一张大的背景图片,是海洋景色(1870px x 756pxdrawable-xxhdpi中),我将其居中显示在所有设备上:

<ImageView 
    android:src="@drawable/bg_image"
    android:scaleType = "centerCrop"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</ImageView>

除此之外,我想再放一张船的图片(500px x 300px,也在drawable-xxhdpi中),水平居中,但应距离屏幕顶部230px以在地平线上。

<ImageView 
    android:src="@drawable/another_image"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="230px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ImageView>

enter image description here

我的结果不正确,而且安卓系统提示我不能使用px作为边距的单位。

结果:设备1(在地平线上进行了调整)

enter image description here

更小的设备2(未在地平线上进行调整):

enter image description here

有什么建议吗?


请指定你得到的结果是什么。 - Aakash
@Aakash,我添加了更多的解释,谢谢。 - Majid Laissi
你需要调整图片大小以实现这个目标。 - Aakash
@Aakash,图片尺寸是固定的,你说的“修复”是什么意思? - Majid Laissi
我认为使用帧布局(Frame layout)比相对布局(Relative layout)更好。你试过吗? - Soham
我是指将高度和宽度固定为特定的dp,而不是使用wrap content或fill parent。 - Aakash
3个回答

1

请确保背景图片的地平线在垂直方向上居中。然后,您可以使用RelativeLayout的简单技巧,在视图的垂直中心位置上方定位您的船只图片。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:src="@drawable/bg_image"
        android:scaleType = "centerCrop"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ImageView>

    <View
        android:id="@+id/center"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <ImageView
        android:src="@drawable/another_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/center">
    </ImageView>
</RelativeLayout>

1
我认为你需要使用“dp”单位……你可以尝试使用不同大小的设备(例如模拟器)来保证它。

我希望这能帮到你


是的,我尝试过 px、dp 和 dip,但在不同的模拟器上它们的位置总是不一样(我有一个平板电脑和两个手机模拟器)。 - Majid Laissi
创建不同的布局怎么样?每个显示器大小都有一个布局。通常,开发人员会这样做。 - iblancasa
我正在尝试避免这种情况,因为那样我就需要对图片进行动画处理,这将是非常繁琐的工作。 - Majid Laissi
是的,我同意,但我认为这是最好的方式。 - iblancasa

0
每个设备都有自己的屏幕分辨率。我认为你正在使用只支持一种屏幕分辨率的船图像。如果你想解决这个问题,你需要添加具有不同分辨率的船图像,例如HDPI、LDPI、MDPI、xhdpi。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接