首页 > 解决方案 > 无法通过支持库使用自动调整 Textview

问题描述

我有以下文本视图,它应该包含自动调整大小的文本。

<TextView
    android:layout_width="0dp"
    android:layout_height="200dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tabRecyclerView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:background="@color/lightshade"
    android:text="@string/add_favorites_message"
    android:gravity="center"
    android:minLines="2"
    android:maxLines="2"
    app:autoSizeTextType="uniform"/>

但是,我收到关于最后一行的错误“意外的命名空间前缀“app””。

我觉得这很奇怪,因为我在我的中使用了以下支持库build.gradle

implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:recyclerview-v7:27.1.1'

我使用这个文档作为我的指南。

我究竟做错了什么?

这是完整的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="250dp"
    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/tabRecyclerView"
        android:layout_width="0dp"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/imageRecyclerView"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/imageRecyclerView"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabRecyclerView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@color/lightshade"
        />

    <TextView
        android:id="@+id/add_favorites_message"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabRecyclerView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@color/lightshade"
        android:text="@string/add_favorites_message"
        android:visibility="gone"
        android:gravity="center"
        android:minLines="2"
        android:maxLines="2" />


</android.support.constraint.ConstraintLayout>

标签: androidtextviewandroid-support-librarysupport-v4

解决方案


我不得不将其更改TextView为:

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/add_favorites_message"
    android:layout_width="0dp"
    android:layout_height="200dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tabRecyclerView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:autoSizeTextType="uniform"
    android:background="@color/lightshade"
    android:text="@string/add_favorites_message"
    android:visibility="gone"
    android:gravity="center"
    android:lines="2"
    />

这个答案对我来说没有意义,因为这篇文章中的第一个答案说我不应该这样做。但至少错误消失了。


推荐阅读