如何根据不同屏幕尺寸的背景在我的 android 应用中定位图像按钮?

How to position the image buttons in my android app with respect to the background for different screen sizes?

我正在制作一个 android 应用程序,我需要将包含位置指针的 3 个图像按钮放置在背景中的道路上,如屏幕截图所示的位置。不幸的是,图像按钮会在不同的屏幕尺寸上改变它们的位置。任何帮助表示赞赏。

以下是我的 XML 代码。现在为了截图,我已经手动给按钮的边距赋值,也因为我不知道任何其他方法。

<FrameLayout 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"
tools:context=".fragment.HomeFragment">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_image"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/ll1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="85dp"
        android:layout_marginStart="85dp"
        android:layout_marginTop="200dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/potheri_button_text"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:layout_marginBottom="5dp"/>
        <ImageButton
            android:id="@+id/potheri_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#00000000"
            android:scaleType="centerInside"
            android:src="@drawable/location_pointer" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="300dp"
        android:layout_marginStart="300dp"
        android:layout_marginTop="200dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/srm_button_text"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:layout_marginBottom="5dp"/>
        <ImageButton
            android:id="@+id/srm_button"
            android:textSize="20sp"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="centerInside"
            android:background="#00000000"
            android:src="@drawable/location_pointer" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll3"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="170dp"
        android:layout_marginStart="170dp"
        android:layout_marginTop="435dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/guduvancheri_button_text"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:layout_marginBottom="5dp"/>
        <ImageButton
            android:id="@+id/guduvancheri_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#00000000"
            android:scaleType="centerInside"
            android:src="@drawable/location_pointer"
            android:text="@string/guduvancheri_button_text"/>
    </LinearLayout>
</RelativeLayout>

尝试约束 layout.They 在调整大小和放置看起来应该相同的组件时非常棒 devices.You 可以在这里找到它 Constraint Layout Code Labs

或者,您可以为不需要 dimen.xml 文件的各种 devices.For 设计屏幕,因为它们可能很棘手。您可以制作不同的布局文件 layout-large、layout-land、layout-xxxhdpi、layout-sw700dp。你可以在这里找到它 Supporting multiple Screens in Android

Mike 在评论中关于使用 x 和 y 坐标的解决方案也可以在这里应用,但为此你可以在你的位置上放置一个透明视图 images.Those 透明视图将根据设备分辨率移动一点,但它们将主要用于接收您的点击事件

更新

<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/bg_image"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1" />

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginStart="6dp"

        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/ll2"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/ll2"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        app:layout_constraintVertical_bias="1.0">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_weight="38.89"
            android:text="Potheri"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/potheri_button"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#00000000"
            android:scaleType="centerInside"
            android:src="@drawable/location_pointer" />

    </LinearLayout>


    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="20dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="101dp" />

    <!--<android.support.constraint.Guideline-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:id="@+id/guideline3"-->
    <!--app:layout_constraintGuide_begin="300dp"-->
    <!--android:orientation="horizontal" />-->

    <LinearLayout

        android:id="@+id/ll2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginStart="32dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintHorizontal_bias="0.38"
        app:layout_constraintLeft_toRightOf="@+id/ll1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView"
        app:layout_constraintVertical_bias="0.36"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintTop_creator="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="SRM"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/srm_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#00000000"
            android:scaleType="centerInside"
            android:src="@drawable/location_pointer"
            android:textSize="20sp" />
    </LinearLayout>

   <TextView
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       android:id="@+id/textView6"
       app:layout_constraintHorizontal_bias="0.0"
       tools:layout_constraintRight_creator="1"
       tools:layout_constraintLeft_creator="1"
       app:layout_constraintTop_toTopOf="@+id/guideline2"
       android:layout_marginTop="0dp"
       app:layout_constraintVertical_bias="0.68" />
    <LinearLayout
        android:id="@+id/ll3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        android:layout_marginTop="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="Gudvancheri"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/guduvancheri_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#00000000"
            android:scaleType="centerInside"
            android:src="@drawable/location_pointer"
            android:text="Gudvancheri" />
    </LinearLayout>



</android.support.constraint.ConstraintLayout>

以上是我在给定情况下所能做的最好的,如果可行,请告诉我。

或者,您可以将图像分成 3 部分,为 ll3ll2ll1 设置锚点。这样无论图像拉伸多少,布局都会始终固定在特定位置。