2015-05-29 36 views
1

蓝色的矩形是不可见的区域(使调试可见),我点击并发生了一些事情。在IDE上,他们是在正确的地方。 fff如何让ImageView出现在不同显示器上的相同位置?

但在genymotion(相同的模型)他们被放置在一个不同的位置 - 为什么?什么可能是问题 - 屏幕拒绝? qqq

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:id="@+id/main"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/background" 
    android:src="@drawable/page_2_bkg" 
    android:layout_alignParentTop="true" /> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/door_closed2" 
    android:src="@drawable/page2_door_closed" /> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/door_open2" 
    android:src="@drawable/page2_door_opened" 
    android:visibility="invisible" /> 

<ImageView 
    android:layout_width="80dp" 
    android:layout_height="300dp" 
    android:id="@+id/boy" 
    android:layout_marginLeft="160dp" 
    android:visibility="visible" 
    android:src="#ff2b16ff" 
    android:layout_marginBottom="50dp" /> 

<ImageView 
    android:layout_width="150dp" 
    android:layout_height="300dp" 
    android:id="@+id/doorBox" 
    android:visibility="visible" 
    android:layout_alignParentTop="true" 
    android:layout_alignRight="@+id/background" 
    android:layout_alignEnd="@+id/background" 
    android:layout_marginRight="118dp" 
    android:layout_marginEnd="118dp" 
    android:src="#ffff3240" /> 

+0

你的xml代码在哪里? – Josef

+0

@Josef,我现在编辑它 – ERJAN

+0

两个蓝色矩形的id是什么? – Josef

回答

0

屏幕密度仅仅是一种可能性,并有可能的贡献因素之一。另一个可能是,你正在放置蓝色矩形与屏幕相关,但你应该做的是将其与图片相关联。

这得到与屏幕密度稍微困难一些,因为100px的2倍密度的屏幕上只会移动矩形只要它需要,因此而不是使用像素,考虑与百分比工作。它使得精神上的处理变得更容易。

例如,它看起来像'门'长方形从图像宽度的75%,到图像宽度的90%。它大约是图像高度的15%,大约是图像高度的75%。

知道所有(伪码示例):

  • DoorRectangle.left = Image.left +(Image.width * 0.75)
  • DoorRectangle.right = Image.left +(Image.width * 0.90 )
  • DoorRectangle.top = Image.top +(Image.height * 0.15)
  • DoorRectangle.bottom = Image.top +(Image.height * 0.75)
1

我看到第在您使用XML布局时,在您编辑之前,它并不在您的原始问题中。无论如何,问题在于矩形与屏幕相关,当它应该被放置在与图像相关的位置。

可以实现通过执行以下操作这样的效果:

  • 添加新RelativeLayout屏幕布局内(这也恰好是一个RelativeLayout
  • 调整大小和重新定位RelativeLayout到中心和消费满屏幕大小等
  • 让新RelativeLayout背景图片的图片
  • 将是RelativeLayout和位置将永远是马内的矩形相对进入。这就是RelativeLayout中的relative意味着;)
  • 作为一个侧面说明,'dp'单元应该考虑密度。

你要做的是让矩形在图像的前部分,但是当你调整图像大小/移动图像时,矩形无法知道'父'图像的尺寸前置部分即使它不是真正的父母,这是问题的一部分)。通过使两个对象相对于父对象移动,您始终保持正确的位置。

+0

“dp单元应该考虑密度”?我如何调整它? – ERJAN

+1

道歉,我指的是'margins',即android:layout_marginLeft =“100dp”,android:layout_marginTop =“100dp”。您可以在Eclipse布局预览属性面板中编辑/添加这些元素,或者通过将边距自己添加到矩形的XML中。将很快更新答案以反映这一点。 – 1owk3y

相关问题