【发布时间】:2019-03-27 18:31:07
【问题描述】:
我正在开发一个显示停车场并显示可用停车位的应用程序。但是,在不同设备中显示可用停车位时遇到问题。背景图像是停车场的草图,但是通过在停车位顶部添加一个红色框来显示停车位。但是当我使用不同的设备时,红框会不断改变位置。
首先,这是我的 XML 文件,我在其中上传了背景中的图像,并为必须放置在背景图像顶部的一些框设置了位置。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Map.LotDisplay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:id="@+id/map"
android:orientation="horizontal"
android:background="@drawable/parkingmap">
</LinearLayout>
<Spot
android:id="@+id/spot0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="344dp"
android:layout_marginTop="250dp"
app:height="14dp"
app:width="33dp" />
<Spot
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spot1"
app:height="14dp"
app:width="33dp"
android:layout_marginTop="266dp"
android:layout_marginLeft="344dp"/>
</RelativeLayout>
这是图像的外观:
但是,当我使用不同的设备时,它是这样的:
谁能告诉我为什么会发生这种情况以及为什么要修复它?我认为使用 dp 应该可以解决问题,但事实并非如此。
这是 XML 文件中引用的 parkmap.png:
谢谢!
【问题讨论】:
-
这是设计风格。边距是恒定的,但屏幕尺寸不是。 300 dp 的边距将图像放置在 330 dp 宽的设备上与 700 dp 宽的设备上完全不同的位置
-
你可以试试PercentRelativeLayout,这可以帮助你“android.support.percent.PercentRelativeLayout”