2015-06-23 136 views
3

在我RelativeLayout,我有一个海景(在drawable-xxhdpi1870px X 756px),我中心所有设备的大背景图片:ImageView的固定位置

<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

我没有得到正确的结果和Android抱怨使用px为单位的利润率最高。

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

enter image description here

较小的器件2(未在地平线上调整):

enter image description here

任何建议?

+0

什么是你所得到的结果,请注明 – Aakash

+0

@Aakash我增加了更多的解释,谢谢 –

+0

需要修复的图像大小为实现这一 – Aakash

回答

1

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

我希望这将有助于你

+0

是的,我尝试过px,dp和dip,它从来没有在不同的模拟器(我有一个平板电脑和两个手机模拟器)的相同位置。 –

+0

如何创建不同的布局?每种显示器尺寸的一种布局。通常情况下,开发人员会这样做 – iblancasa

+0

我试图避免这种情况,因此我需要对图片进行动画制作,这将是非常乏味的工作 –

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

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