【问题标题】:Image changes position with different devices图像随不同设备改变位置
【发布时间】: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”

标签: android android-layout


【解决方案1】:

请试试这个。希望这适用于多种屏幕类型

<android.support.constraint.ConstraintLayout 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">

<android.support.constraint.ConstraintLayout
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/map"
    android:orientation="horizontal"
    android:scaleType="centerInside"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">




    <Spot
        android:id="@+id/spot0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/_26sdp"
        android:layout_marginBottom="@dimen/_100sdp"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spot
        android:id="@+id/spot1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/_26sdp"
        android:layout_marginBottom="@dimen/_135sdp"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

将此添加到 Gradle 文件中

implementation 'com.intuit.sdp:sdp-android:1.0.6'

【讨论】:

  • 谢谢!它没有完全起作用,但它引导我朝着正确的方向前进!
【解决方案2】:

首先,我很抱歉我将其发布为答案,因为我无法发表评论(我没有 50 名声望)。

很明显,位置会发生变化,因为您正在硬编码 layout_marginTopmarginLeft 值,这应该是所有设备通用的。

但是有一个解决方案,试试这个库android sdp

这个库可以让你使用 sdp 代替 dp 作为一个单元。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多