【发布时间】:2018-06-03 09:41:53
【问题描述】:
几周以来,我一直在尝试解决如何让我的 UI 文本支持各种不同的设备分辨率和密度的问题,但没有成功。在阅读了开发者指南和multiple stack posts之后,我尝试了很多东西。首先,我创建了layout directories,您可以在下图中看到(layout-sw320dp、layout-sw360dp 和layout-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)
与他们取得联系后,这些是他们发回给我的图片(不是每个人都回答过):
我还有这些代码行可以帮助我检查手机是 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