我有一段简单的代码,我希望在其中调整 TextView 内容的大小。在最初的印象中,TextView 中的 Text 的内容缩小了,这很好,但是在启用布局边界后,我发现具有 AutoSizing 的 TextView 实际上比不用于相同内容的 TextView 在屏幕上占用更多空间。>
这是我的代码:
<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/tv_1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:text="Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello"
app:autoSizeMaxTextSize="12sp"
app:autoSizeMinTextSize="4sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tv_2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tv_1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我错过了什么吗?任何输入将不胜感激。
答案 0 :(得分:0)
在 Android 8.0(API 级别 26)及更高版本中,您可以指示 TextView 让文本大小自动扩展或收缩以根据 TextView 的特征和边界填充其布局。
默认设置允许 TextView 的自动大小在水平和垂直轴上均匀缩放。
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview