找到你要的答案

Q:ImageView in fixed position

Q:在固定位置的图片

In my RelativeLayout, I have a large background picture of an ocean view (1870px x 756px in drawable-xxhdpi) that I center for all the devices :

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

On top of it, I want to put another Image of a ship (500px x 300px also in drawable-xxhdpi), centred horizontally, but should be 230px away from the top of the screen to be on the horizon line.

<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>

I'm not getting the correct results and android is complaining about the use of px as a unit for the margin top.

Results : Device 1 (adjusted on the horizon)

Smaller Device 2 (not adjusted on the horizon):

Any suggestions?

在我RelativeLayout,我有一个海景大背景图片(在冲xxhdpi 1870px X 756px),我中心的所有设备:

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

在它的上面,我想把另一船的图像(500px X 300px也冲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>

我没有得到正确的结果,Android是抱怨使用PX作为一个单位的利润顶端。

结果:设备1(调整地平线)

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

有什么建议吗?

answer1: 回答1:

I think you have to use "dp" unit... You could try using diferent devices (emulators, for example) with diferent sizes to guarantee it.

I hope this will help you

我认为你必须使用“DP”单位…你可以尝试使用不同的设备(模拟器,例如)与不同的尺寸来保证。

希望这对你有帮助

answer2: 回答2:

Make sure that the horizon on your background image is centered vertically. Then you can use this simple trick of RelativeLayout that will position your ship image above vertical center of the view

<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>

确保背景图像上的地平线垂直居中。然后你可以使用这个简单的技巧,将你的船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>
answer3: 回答3:

each device has it's own screen resolution. I think you're using boat image with one screen resolution support. if you want to solve this problem you need to add boat image with different resolutions. Like HDPI,LDPI,MDPI,xhdpi

每个设备都有自己的屏幕分辨率。我认为你使用的是一个屏幕分辨率支持的船图像。如果你想解决这个问题,你需要添加不同分辨率的船图像。如华电国际,仪,MDPI,xhdpi

android  android-layout  imageview