我的手机分辨率是 720x1280,我在设计应用程序时在 Android Studio 中使用这个分辨率。我已经复制了下面的图像和 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">
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Államadósság-Kezelő Központ"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="515dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView19"
app:layout_constraintVertical_bias="0.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.6"
android:paddingStart="0dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="A központi költségvetés adóssága:"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/date1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="center"
android:text="2018.11.30"
android:textAlignment="center" />
<TextView
android:id="@+id/data1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="center"
android:text="29005,02 Mrd Ft"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.6"
android:paddingStart="0dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="Maastrichti adósságráta:"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/date2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="center"
android:text="2018.11.30"
android:textAlignment="center" />
<TextView
android:id="@+id/data2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="center"
android:text="72.8%"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
最佳答案
问题是 Android 支持多种不同的设备。查看 Android studio 正在哪个设备上进行“预览”。 预览是您在问题顶部看到的内容。它很可能是与您的模拟器不同的设备。
此外,请注意这一行:android:textSize="18sp"
。您使用 SP 作为维度 -> 这意味着 SCALE 独立像素。拍个look here了解更多信息。但长话短说,SP 考虑了用户的文本大小偏好。这意味着在每部 Android 手机的设置中,您都可以选择将文本大小更改为小、中、大、超大等。因此,设置大的人的文本会占用更多空间比选择小的人。
解决方案
解决这个问题并不简单,我不知道你想要实现什么目标,但你应该尝试使用:
良好的约束(即不让东西重叠)
maxWidth
属性ellipsize
TextViews
属性(如果无法容纳,则在长文本的末尾/中间/开头添加“...”)maxLines
如果您不希望它超过一定数量的行
ConstraintLayout
非常强大,您应该仔细研究它。您可能应该对齐需要对齐的内容的左边缘或右边缘,并将宽度也设置为 0,因为当它知道左边缘和右边缘应该在哪里时,它不需要有宽度。
关于android - 我的 UI 在设计器中和在设备上看起来不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54277591/