我正在为 Android 编写一个非常简单的优惠券应用程序,但我在使用 TextView 时遇到困难。
对于具有 16:9 屏幕的设备,TextView 显示正常,但问题开始于设备,例如我拥有的 Samsung Note 9,它具有 18:9 屏幕。 TextView 不会变大来填充空白区域。
对于 16:9 屏幕,我使用 Google Pixel 2 模拟器,而对于 18:9 屏幕,我使用我自己的三星 Note 9。
有没有人知道如何让我的 TextView 响应,以便它可以占据空白区域?
我的设备的照片:
像素 2 - https://i.imgur.com/HBrqYFM.png 三星 Note 9 - https://i.imgur.com/i1e0AXU.jpg
我尝试使用 AutoSizing TextView,但我不知道如何让它解决我的问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.GeneratedCouponActivity" android:orientation="vertical" android:padding="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentStart="true">
<TextView
android:text="DATA WYDANIA:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="18sp"
android:textStyle="bold" android:textColor="@android:color/black"/>
<TextView
android:text="25/06/2019"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView2" android:textSize="16sp"
android:textColor="@android:color/black"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentEnd="true">
<TextView
android:text="UNIKALNY KOD:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView3" android:textStyle="bold"
android:textSize="18sp" android:textColor="@android:color/black"/>
<TextView
android:text="f-62-pT-6"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView7"
android:textColor="@android:color/black" android:textSize="16sp"/>
</LinearLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="140dp" app:srcCompat="@drawable/coupon_icecream" android:id="@+id/imageView"/>
<TextView
android:text="@string/generated_coupon_rules"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView8" app:autoSizeTextType="uniform"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="bottom">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="95dp" android:layout_marginBottom="10dp" app:cardBackgroundColor="#FFC300"/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="95dp" app:cardBackgroundColor="#FFC300"/>
</LinearLayout>
</LinearLayout>
最佳答案
不同的手机有不同的屏幕尺寸,在您的布局中,您在 View 上使用固定尺寸(例如,固定尺寸为 android:layout_height="140dp"
)并且结果是在一个屏幕(您的 android studio 预览屏幕)上看起来不错的东西在另一个屏幕(您的实际手机)上看起来可能不太好。
如果您想创建一个布局以支持所有屏幕尺寸,您可以使用 ConstraintLayout与 guidelines和 Chains以支持不同的屏幕尺寸。
这是一个使用 ConstraintLayout 的例子:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView5" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/textView8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view I am some long text view "
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline5" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent="0.7" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:srcCompat="@tools:sample/avatars[1]" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Date"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#ff1"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<TextView
android:id="@+id/textView7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#ff1"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="560dp"
app:layout_constraintGuide_percent="0.45" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是它的外观(我附上了布局编辑器中的图像,以便您可以看到约束和指南):
关于android - 如何让 TextView 在屏幕比例更高的设备上响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029641/