我有一个线性布局的 TextView
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="vertical"
android:id="@+id/textLayout"
android:background="@drawable/bg"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="@+id/detail_text"
android:text="hello"
android:background="#55000000"
android:lineSpacingExtra="5dp"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp"
app:autoSizeTextType="uniform"
android:textColor="@android:color/white"
android:layout_margin="10dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
在 build.gradle 中我有
dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.google.android.gms:play-services-ads:11.4.2'
implementation 'com.android.support:support-compat:26.0.0'
}
但它仅显示固定大小(最小大小)的文本。它不会根据手机宽度自动调整大小;
最佳答案
不建议将 wrap_content
用于 layout_height
或 layout_width
,因为它可能不起作用。在您的情况下,您需要为 layout_height
使用固定值。
来自 docs
If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results.
关于java - TextView 不是自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419528/