TextView autoSizeTextType在Compat中不起作用

Dea*_*inY 8 xml android textview android-support-library

我试过用autoSizeTextType.我的minSdk是24,所有的工具都是26,compat也是 26-beta2.

通过代码尝试使它们可调:

dialogWeight.touchables.filterIsInstance<Button>().forEach {
TextViewCompat.setAutoSizeTextTypeWithDefaults(it, 
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM) }
Run Code Online (Sandbox Code Playgroud)

和xml:

<android.support.v7.widget.AppCompatTextView
android:layout_height="match_parent"
android:text="7"
android:autoSizeTextType="uniform"/>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?我开始相信它目前被窃听了

Dea*_*inY 17

好的,所以有效的设置组合:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.AppCompatTextView
android:text="7"
app:autoSizeTextType="uniform"/>
Run Code Online (Sandbox Code Playgroud)

您还需要appcompat-v7库作为模块build.gradle文件中的依赖项.

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*ick 9

我以编程方式解决了它。

TextView number1 = findViewById(R.id.number_one);
TextViewCompat.setAutoSizeTextTypeWithDefaults(number1, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Run Code Online (Sandbox Code Playgroud)

和 XML:

  <TextView
        android:id="@+id/number_one"
        android:autoSizeTextType="uniform"
        android:gravity="center"
        android:text="1"  />
Run Code Online (Sandbox Code Playgroud)


JHo*_*zer 5

要理解的关键是使用app:autoSizeTextType而不是android:autoSizeTextType

根据文档

要通过支持库在XML中定义默认设置,请使用应用程序名称空间,并将autoSizeTextType属性设置为none或统一。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)