ImageView in fixed position

In mine RelativeLayout

, I have a large ocean view background image ( 1870px

x 756px

in drawable-xxhdpi

) that I am the hub for all devices:

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

      

In addition to this, I want to place another ship image ( 500px

x 300px

also at drawable-xxhdpi

), centered horizontally, but must be 230px

away from the top of the screen to be on the horizon.

<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

I am not getting correct results and android is complaining about using px

as unit for top margin.

Results: Device 1 (configured on the horizon)

enter image description here

Smaller device 2 (not tuned on horizon):

enter image description here

Any suggestions?

+3


source to share


3 answers


I think you need to use the "dp" block ... You can try different devices (eg emulators) with different sizes to guarantee it.



I hope this helps you

+1


source


Make sure the horizon in the background image is centered vertically. Then you can use this simple trick RelativeLayout

to position your ship image above the 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>

      

+1


source


each device has its own screen resolution. I think you are using a single screen supported boat image. if you want to solve this problem you need to add a boat image with different resolutions. Like HDPI, LDPI, MDPI, xhdpi

0


source







All Articles