【问题标题】:Adjusting text to support multiple screens调整文本以支持多个屏幕
【发布时间】:2018-06-03 09:41:53
【问题描述】:

几周以来,我一直在尝试解决如何让我的 UI 文本支持各种不同的设备分辨率和密度的问题,但没有成功。在阅读了开发者指南multiple stack posts之后,我尝试了很多东西。首先,我创建了layout directories,您可以在下图中看到(layout-sw320dplayout-sw360dplayout-sw400dp):

现在,如果我在几个不同的 emulators 上测试我的应用程序,UI 的文本 看起来不错,但是当我的 Alpha 测试人员在他们的手机上下载该应用程序时,UI 的文本 放错了位置。这是我试图在所有设备上实现的外观:

下面我添加了 alpha 测试人员使用的 7 种不同设备:

1) S8+ (2960x1440)(529dpi)

2) 三星 Galaxy j7 Prime (1920x1080) (401dpi)

3) S8+ (1440x2960)(~529dpi)

4) 三星 A3 (960x540)(245dpi)

5) Motor z play Droid (1920x1080) (403 dpi)

6) 三星 Galaxy S7 (1440x2560) (577dpi)

7) 索尼 Xperia xa (720x1280)

与他们取得联系后,这些是他们发回给我的图片(不是每个人都回答过):

2)

5)

6)

7)

我还有这些代码行可以帮助我检查手机是 s8 还是 s8+(因为它们的 18:9 比例需要不同的布局样式)。这会导致问题吗?

        // Get the user's phone's height and width
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    // screen size in inches
    int height = dm.heightPixels;
    int width = dm.widthPixels;
    double x = Math.pow(width/dm.xdpi,2);
    double y = Math.pow(height/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("SIZE_INCHES", screenInches + "\"");

    // If it's an S8 or S8+ choose the appropriate layout
    if (height > 1920 && height <= 2220) { // FHD+ (2220x1080)
        if(screenInches <= 6.0) {
            setContentView(R.layout.cardview_s8); //s8
        } else {
            setContentView(R.layout.cardview_s8_plus); // s8+
        }
    } else if (height > 2220 && height <= 2960) { // S8 WQHD+ (2960x1440)
        if(screenInches <= 6.0) {
            setContentView(R.layout.cardview_s8_wqhd_res);
        } else {
            setContentView(R.layout.cardview_s8_plus_wqhd); // s8+
        }
    } else { // otherwise choose the appropriate layout for the user's phone based on it's swdp qualifier
        setContentView(R.layout.cardview);
    }

这是我的布局(设计视图)供参考:

这是 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/card_view_bg"
tools:layout_editor_absoluteY="25dp">

<ImageView
    android:id="@+id/cardArtImageView"
    android:layout_width="400dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/cardDetailsImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/cardDetailsImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/card_details_box" />

<TextView
    android:id="@+id/leaderSkillDesc"
    android:layout_width="300dp"
    android:layout_height="19dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="40dp"
    android:layout_marginTop="8dp"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fontFamily="monospace"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textAlignment="viewStart"
    android:textColor="@color/white"
    android:textSize="13sp"
    android:textStyle="italic"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.496"
    app:layout_constraintStart_toStartOf="@+id/cardDetailsImageView"
    app:layout_constraintTop_toTopOf="@+id/guideline8"
    app:layout_constraintVertical_bias="0.626" />

<TextView
    android:id="@+id/superAttackDesc"
    android:layout_width="314dp"
    android:layout_height="21dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="40dp"
    android:layout_marginTop="8dp"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fontFamily="monospace"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textAlignment="viewStart"
    android:textColor="@android:color/white"
    android:textSize="13sp"
    android:textStyle="italic"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline9" />

<TextView
    android:id="@+id/superAttackTitle"
    android:layout_width="250dp"
    android:layout_height="15dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:fontFamily="monospace"
    android:textAlignment="viewStart"
    android:textColor="@android:color/holo_blue_light"
    android:textSize="14sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/superAttackDesc"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="443dp" />

<android.support.constraint.Guideline
    android:id="@+id/guideline9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="596dp" />

</android.support.constraint.ConstraintLayout>

我在这里做错了什么?在我的安卓开发生活中,我从来没有像现在这样困惑过……这份工作有更明确的方法吗?我需要更多layout dirs 或其他什么吗?那 3 个layout dirs 正确吗?如果您知道任何可以帮助我的事情,请在下面发布。

PS:我正在为这部分 UI 使用约束布局,这些约束会导致问题吗?

【问题讨论】:

  • 布局目录的全部意义在于不要像多次调用 setContentView 那样做。所以你肯定在某个地方感到困惑。为了获得任何帮助,您需要提供很多关于您如何进行绘图的详细信息 - 有多少视图,什么是位图以及什么是开放的 gl,哪些/如何拉伸某些位图以完全适合屏幕以及哪些不是,等等。如果您在像这样的高度图形化设计上寻找所有设备上的像素完美布局,您需要仔细考虑。
  • @GabeSechan 在我的情况下位图是否重要,因为问题出在文本上?是的,我很确定我对整个事情都感到困惑。
  • 您在位图上书写文本。如果你想摆脱你正在写文字的 1 张巨型图像 - 是的,你会度过一段糟糕的时光。
  • 哦,对了,我才意识到这一点。感谢您指出。可以做些什么呢?图像拉伸是否导致文本的奇怪行为?
  • 想象一下你有一个用于底部的RelativeLayout,它的背景只是时髦的矩形。然后你可以说,相对于那个观点,把不同的东西放在里面。由于您是通过布局完成所有这些操作,因此您可以准确地告诉它放置文本视图的位置 - 例如说布局文本并将其顶部与显示“领导技能”的视图顶部对齐。并把上面写着“特殊攻击”的东西放在下面。等等。

标签: android xml android-layout android-studio android-emulator


【解决方案1】:

@Gabe Sechan 已经解决了这个问题(任何人都可以回来查看),请查看帖子下方的 cmets!

【讨论】:

    【解决方案2】:

    您似乎有一张背景图片,并且您正试图将文本放置在该背景图片上的正确位置。您可以将背景图像拆分并用作按钮背景图像吗?更好地使用回收站视图?我现在意识到这些碎片可能无法正确对齐整个图像。艺术家有空吗?祝你好运,我知道让事情在每台设备上运行是多么困难。你似乎对这个概念有很好的理解

    【讨论】:

    • RecyclerView 将如何帮助他?没有重复的元素可以回收。
    • 是的,您可以使用多种视图类型进行创建——但如果没有重复,则使用回收器视图将无济于事。在他的设计中,没有任何东西需要回收者视图。如果您建议使用 RecyclerView 的布局功能,那是我听过的最糟糕的使用建议。
    • @Pomagranite 嗨,伙计,就像我在标题和操作中提到的那样,我对图像没有任何问题,但文本没有正确对齐;)
    • @Stelios Papamichael 你尝试过测量吗developer.android.com/guide/practices/screens_support.html“使用新的尺寸限定符”
    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多