8

我对在 Android Studio 中添加正确的支持库以便能够使用 AutoSizing TextViews 感到困惑。 https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html
在此页面上对要使用的支持库的描述有点模糊。我已尝试导入建议的库,但要么我请求了错误的库(找不到它们,例如 android.support.v4.widget 包),要么我做错了其他事情。

我的最小 SDK 为 21,最大 SDK 为 27,因此如果我导入了正确的库,AutoSizing 功能应该向后兼容我的应用程序。但是,在屏幕设计视图中,我收到警告“属性 autoSizeTextType 仅用于 API 级别 26 及更高级别(当前最小值为 21)”

据我所知,我包含了正确的支持库。项目结构依赖如下:

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'android.arch.lifecycle:compiler:1.0.0'
implementation 'android.arch.persistence.room:runtime:1.0.0'
implementation 'android.arch.persistence.room:compiler:1.0.0'
implementation 'android.arch.paging:runtime:1.0.0-alpha4-1'
implementation 'android.arch.core:core-testing:1.0.0'
implementation 'android.arch.persistence.room:testing:1.0.0'
implementation 'android.arch.lifecycle:common-java8:1.0.0'
implementation 'com.android.support:support-v13:27.0.2'
implementation 'com.android.support:appcompat-v7:27.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2-alpha1'
implementation 'com.android.support:support-annotations:27.0.2'
implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.android.support:preference-v14:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'android.arch.lifecycle:extensions:1.1.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
implementation 'com.android.support:preference-v7:27.0.0'
implementation 'com.android.support:support-compat:27.0.0'

有什么建议么?我还可以使用一些链接来了解如何将建议的库转换为要导入的实际库。谢谢

编辑:带有 android:autoSizeTextType 问题的部分布局列表。我收到列出的两个文本视图的警告消息。

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

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="134dp"/>

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/temperature_label"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="@string/temperature"
    android:autoSizeTextType="uniform"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<TextView
    android:id="@+id/temperature"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:hint="@string/temperature"
    android:autoSizeTextType="uniform"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent"/>

编辑:我现在的解决方案。

这种布局是我最终得到的,现在似乎可行。不确定 autoSizing 是否正在调整大小,但这在我目前正在测试的设备上看起来不错。按照建议使用 app: autoSizeTextType 前缀尝试纯 TextView,但收到错误“为标签 TextView 找到的意外命名空间前缀“app””。当我将其更改为 android.support.v7.widget.AppCompatTextView 时,错误消失了。谢谢您的帮助。

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

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.38"/>

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/temperature_label"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:text="@string/temperature"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    app:autoSizeTextType="uniform"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/temperature"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:hint="@string/temperature"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    app:autoSizeTextType="uniform"
    app:layout_constraintLeft_toRightOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent"/>
4

4 回答 4

14

改变这个:

<android.support.v7.widget.AppCompatTextView
    android:autoSizeTextType="uniform"
    .../>

对此:

<TextView
    app:autoSizeTextType="uniform"
    .../>

没有必要AppCompatTextView直接引用。支持库LayoutInflater使用的 将自动注入 AppCompat 小部件来代替标准小部件。

此外,要在 API-26 之前实现自动调整大小,您需要使用支持库实现,这意味着您需要使用 AppCompat 属性(带有app:命名空间)而不是平台属性(带有android:命名空间)。

于 2018-02-28T22:07:17.417 回答
5

尝试更改android:app:命名空间:

<android.support.v7.widget.AppCompatTextView
    app:autoSizeTextType="uniform"
    .../>
于 2018-10-01T08:50:15.373 回答
3

关于是否使用有矛盾的答案:

<android.support.v7.widget.AppCompatTextView
    app:autoSizeTextType="uniform"
    .../>

或者:

<TextView
    app:autoSizeTextType="uniform"
    .../>

差异与使用的 LayoutInflater 有关。例如,如果您使用的是 AppCompatActivity,则 TextView 会自动替换。但是,如果您使用的是 FragmentActivity(从 AppCompatActivity 扩展而来),则不会,因此您需要直接使用 AppCompatTextView 来利用这些功能。

app在任何一种情况下,它都不应该android用于 26 之前的构建 - 但您可以在单个布局中提供两者。

此外,您也可以同时使用样式:

    <style name="TextDefault" parent="@android:style/TextAppearance.DeviceDefault">
        <item name="android:autoSizeTextType" tools:ignore="NewApi">uniform</item>
        <item name="autoSizeTextType">uniform</item>
    </style>

app请注意,在样式中使用时,该名称不包括26 之前的版本。

于 2019-01-21T15:50:20.110 回答
1

自动调整大小在支持库中。使用 AppCompatTextView 而不是常规的 TextView。

于 2018-02-27T20:44:27.087 回答