我对在 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"/>