【问题标题】:Support libraries to add for Autosizing TextViews为自动调整 TextViews 添加支持库
【发布时间】:2018-02-27 20:12:47
【问题描述】:

我对在 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,因此如果我导入了正确的库,自动调整大小功能应该向后兼容我的应用程序。但是,在屏幕设计视图中,我收到警告“属性 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"/>

【问题讨论】:

  • 您能否更新您的问题以包含包含 &lt;TextView&gt; 标记的布局,您要在其中尝试使用自动文本大小调整?
  • 好的。见上面的代码示例。

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


【解决方案1】:

改变这个:

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

到这里:

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

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

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

【讨论】:

  • 感谢您的建议。虽然这个确切的例子对我不起作用(参见上面的编辑解决方案),但它确实让我尝试了一些额外的设置,让我找到了我的解决方案,所以我会把它当作答案。顺便说一句,我尝试在代码中设置 autoSizeTextType (setAutoSizeTextTypeWithDefaults(int autoSizeTextType) ),但在 TextView 中遇到了相同类型的问题。
  • 我必须将我的文本视图设置为 AppCompatTextView 才能识别该属性。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    或:

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

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

    在任何一种情况下,对于 26 之前的版本,它都应该是 app 而不是 android - 但您可以在一个布局中同时提供两者。

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

        <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 之前的版本。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 已经试过了。仍然收到警告消息。请参阅上面对初始问题的编辑。谢谢
      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 2015-08-17
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多