android - 支持为自动调整 TextViews 添加的库

标签 android android-layout android-studio android-support-library import-libraries

我对在 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 功能应该向后兼容我的应用。但是,在屏幕设计 View 中,我收到警告“属性 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 问题。我收到了列出的两个 TextView 的警告消息。

<?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: prefix for 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"/>

最佳答案

改变这个:

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

为此:

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

无需直接引用 AppCompatTextView。支持库使用的 LayoutInflater 将自动注入(inject) AppCompat 小部件代替标准小部件。

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

关于android - 支持为自动调整 TextViews 添加的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017163/

相关文章:

android - 修复了可滚动 RelativeLayout 中的 FAB 按钮

javascript - ionic : Crosswalk crashing on Samsung S6 device

java - Android Listview 无法正常工作?

android - weightSum 不能在 android 中正确分割屏幕

android - 我希望布局的 subview 随媒体播放器当前位置自动更改

android-studio - 没有配置 Koin 上下文。请使用 startKoin 或 koinApplication DSL

java - Android Studio - 任务 ':app:transformClassesWithDexForDebug' 执行失败 - java.exe 以非零退出值 2 完成

java - Android Studio - 'Cannot resolve symbol' 语法高亮显示错误,尽管构建正常

android - 如何从uri获取路径? android开发

android - 如何通过单击图像按钮动态设置编辑文本可编辑?