【问题标题】:How do I make my android app scale properly accross different devices?如何使我的 android 应用程序在不同设备上正确扩展?
【发布时间】:2021-03-17 15:59:24
【问题描述】:

我对 android 开发很陌生,我目前正在为九人莫里斯游戏制作应用程序,我正在使用自定义布局 xml 文件,其中包含 1 个用于游戏板的图像视图,然后是 23 个其他图像视图用于板上所有不同的点,都在一个约束布局内,然后我使用标签将此布局包含在我的主要活动布局中。

所有图像视图使用 AVD 在像素 4 上看起来完美对齐,但当我在旧设备上测试时它们看起来并不对齐,所以你们认为让图像视图在所有屏幕尺寸上完美对齐的最佳方法是什么? this pic 显示了布局的外观,下面我将发布前 4 个图像视图的代码

<androidx.constraintlayout.widget.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:id="@+id/g_items">

<ImageView
    android:id="@+id/the_board"
    android:layout_width="376dp"
    android:layout_height="374dp"
    android:paddingEnd="15dp"
    android:paddingStart="15dp"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/board" />

<ImageView
    android:id="@+id/point_0"
    android:onClick="onTileClicked"
    android:layout_width="65dp"
    android:layout_height="56dp"
    android:layout_marginStart="4dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/the_board"
    app:srcCompat="@drawable/empty_spot"/>

<ImageView
    android:id="@+id/point_1"
    android:onClick="onTileClicked"
    android:layout_width="65dp"
    android:layout_height="56dp"
    android:layout_marginStart="104dp"
    app:layout_constraintStart_toEndOf="@id/point_0"
    app:layout_constraintTop_toTopOf="@+id/the_board"
    app:srcCompat="@drawable/empty_spot" />

<ImageView
    android:id="@+id/point_2"
    android:onClick="onTileClicked"
    android:layout_width="65dp"
    android:layout_height="56dp"
    android:layout_marginStart="104dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/point_1"
    app:layout_constraintTop_toTopOf="@+id/the_board"
    app:srcCompat="@drawable/empty_spot" />``

【问题讨论】:

    标签: android xml kotlin layout scaling


    【解决方案1】:

    您需要使用带有 ConstraintLayout 的指南小部件来告诉您的视图在调整大小时何时停止(使用 app:layout_constraintGuide_percent 属性)。

    Google documentation

    【讨论】:

      【解决方案2】:

      ImageViews 的widthheight 设置为0,这样布局将使用约束而不是绝对值。 还将所有约束添加到每个视图(开始、顶部、结束、底部)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        • 2011-01-06
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        相关资源
        最近更新 更多