【问题标题】:TextView autoSizeTextType is not detecting in xmlTextView autoSizeTextType 未在 xml 中检测到
【发布时间】:2017-11-08 16:45:34
【问题描述】:

我正在寻找自动更改 TextView 大小。我也找到了解决方案。这是用于自动调整文本视图大小的官方Doc。但我仍然无法解决它。当我粘贴 autoSizeTextType 时,它​​在 xml 文件中显示错误。

这是我的xml代码和gradle代码sn-ps

我的活动.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

<include layout="@layout/toolbar" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:autoSizeTextType="uniform"/>


</LinearLayout>

Gradle sn-p

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:support-v4:25.2.0'//Added support library
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    testCompile 'junit:junit:4.12'
}

【问题讨论】:

  • Autosizing TextViews 是 Android O 的一项新功能(下一个 Android 版本尚未发布),如文档中所述,尚不适用于以前的版本。这就是它向您显示错误的原因。

标签: android xml textview android-support-library


【解决方案1】:

使用 AppCompatTextView 和 supportLibrary 26.0.1

<?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">

    <android.support.v7.widget.AppCompatTextView
       android:layout_width="match_parent"
       android:layout_height="200dp"
       app:autoSizeTextType="uniform" />

</LinearLayout>

【讨论】:

    【解决方案2】:

    docs中所述:

    支持库 26.0 完全支持运行 Android 8.0(API 级别 26)之前的 Android 版本的设备上的自动调整 TextView 功能。该库提供对 Android 4.0(API 级别 14)及更高版本的支持。 android.support.v4.widget 包包含 TextViewCompat 类以向后兼容的方式访问功能。

    您需要将 TextView 替换为 AppCompatTextView 并将您的支持库升级到 v26.0.0 才能使用该功能。

    compile 'com.android.support:support-v4:26.0.0'
    

    别忘了将您的buildToolsVersion 升级到26.0.0compileSdkVersion26

    【讨论】:

    • 已完成所有步骤。同样的问题Error:(17) No resource identifier found for attribute 'autoSizeTextType' in package com.package.abc
    【解决方案3】:

    此 API 仅适用于 API 级别 26。

    【讨论】:

    • 是的。但是有一个支持库来支持 Android 4.0(API 级别 14)及更高版本
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多