支持为自动调整 TextView 大小添加库

2024-02-24

我对在 Android Studio 中添加正确的支持库以便能够使用 AutoSizing TextViews 感到困惑。https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html 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 是否正在调整大小,但这在我目前正在测试的设备上看起来不错。按照建议,尝试使用 autoSizeTextType 的 app: 前缀来使用纯 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"
    .../>

to this:

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

支持为自动调整 TextView 大小添加库 的相关文章

随机推荐

  • 创建新的 WPF 项目时已添加具有相同键的项目

    我刚刚创建了一个新的 WPF 我在 Visual Studio 2015 Pro 中创建了一个项目 在 创建项目 对话框消失后 我没有得到设计器 这个 ArgumentException 只是显示在那里 我没有添加任何控件或任何东西 刚刚创
  • 空基类优化现在是强制优化吗(至少对于标准布局类)?

    根据C 11 9 1 7 草案 n3376 a 标准布局class 是一个类 没有非标准布局类 或此类类型的数组 类型的非静态数据成员或引用 没有虚函数 10 3 和虚基类 10 1 对所有非静态数据成员具有相同的访问控制 第 11 条 没
  • 连续按下视图控制器时缺少导航或后退按钮标题

    我有一个问题UINavigationController当连续推动视图控制器时 有关信息 我使用 XCode 7 0 针对 iOS 8 进行构建 并在 Simulator 9 0 上运行应用程序 这是用户手动点击表格视图单元格时的视图 如上
  • 使用 concat() 将行附加到数据框

    我定义了一个空数据框 df pd DataFrame columns Name Weight Sample 并想要在 for 循环中追加行 如下所示 for key in my dict row Name key Weight wg Sam
  • 子类是否应该调用直接超类中指定的初始值设定项?

    我看到了一些示例代码 这让我想知道如何在超类中调用指定的初始值设定项 假设我有一些代码 interface NewTableViewCell UITableViewCell end implementation NewTableViewCe
  • Google 是否允许其他人使用他们的“您是说”API 吗?

    我查遍了互联网 但没有找到确切的API 我想在我自己的网站上使用他们的 你的意思是吗 功能 Pygoogle 有一个 api 调用 http pygoogle sourceforge net dist doc public google m
  • Magento Memcached 会话

    我目前正在尝试使用以下设置为 Magento 设置生产环境 2 个 Web 服务器 1 个数据库服务器 负载均衡器 因此 负载均衡器将在 Web 服务器之间分配流量 但不会使用粘性会话 为了解决服务器之间共享会话的问题 我选择使用 Memc
  • Tensorflow 2.1 满内存和 tf.function 被调用两次

    我正在使用 Tensorflow 2 1 开发卷积自动编码器 这是代码 class ConvAutoencoder def init self input shape latent dim self input shape input sh
  • 关闭选项菜单不起作用?

    我只想使用以下代码在特定选项卡中显示选项菜单 public class MainActivity extends FragmentActivity implements ActionBar TabListener The link andr
  • 在android studio中将Unity场景显示为子视图

    我对在 android studio 项目中显示 3D 模型和图形有点困惑 我一直在与Rajawali https github com Rajawali Rajawali 这是一个 Android OpenGL ES 2 0 3 0 引擎
  • 找出重复数字的重复次数

    这是我和朋友们一起写的算法 在 stackoverflow 网站上 该算法将仅找到第一个重复的数字并返回它 这适用于O n 我想完成这个算法 帮助我通过重复获得重复的数字 认为我有 1 1 3 0 5 1 5 我希望这个算法返回2重复的数字
  • 从聚合返回整个文档

    我使用以下查询来获取数据库中每篇文章的最新评论 db comments aggregate match post id in ObjectId 52c5ce24dca32d32740c1435 ObjectId 52c5ce24dca32d
  • jRails 与原型

    我并不是想让这个问题成为一个偏好问题 我真的想知道人们使用 jQuery 和 Rails 或 jRails 进行开发的经验如何 到目前为止 包括我在内的大多数 Rails 用户都在使用 Prototype 然而 我混合了很多 jQuery
  • “iframe沙箱”技术安全吗?

    Update 由于这个问题没有答案 我稍微改变一下问题 下面链接的 Dean 博客上的帖子评论表明该技术在 Safari 中不起作用 我现在的问题是 下面描述的技术在现代浏览器中是否有效 特别是有人可以确认它是否在 Safari 中有效 这
  • 原始资源与 SQLite 数据库

    我正在创建一个应用程序 它将使用大量数据 无论出于何种意图和目的 这些数据都是静态的 我原以为使用 SQLite 数据库来处理该数据是最有意义的 我想知道仅使用 XML 文件然后将其作为原始资源访问是否有意义 请记住 可能会有大量数据 大约
  • Apollo Server - 关于缓存/数据源选项的混淆

    文档 https www apollographql com docs apollo server features data sources html Using Memcached Redis as a cache storage ba
  • “向您的机器人发送此消息时出错:HTTP 状态代码未找到”

    我用 C 创建了一个机器人并部署了它 但我不断收到错误代码NotFound或者在模拟器 404 中测试时 该机器人在测试时在本地工作得很好 但在部署时却失败了 我用 Composer 创建了一个机器人 它做同样的事情 在本地工作 但在部署到
  • 模拟角度服务类属性或变量

    我正在对一个角度应用程序进行单元测试 并且有一个我需要模拟的服务 我能够毫无问题地模拟服务方法 但是当我尝试以相同的方式模拟属性时 它会给我错误 我的配置服务有一个属性和一种方法 我想模 拟该属性 因为我无法生成该值 Service Inj
  • 如何解决 bazel“未声明的包含”错误?

    我是 bazel 新手 并且无法使用以下命令构建我的 C 包 错误 path to package BUILD linenumber 1 规则 path to package name 中未声明包含内容 此规则缺少 path to pack
  • 支持为自动调整 TextView 大小添加库

    我对在 Android Studio 中添加正确的支持库以便能够使用 AutoSizing TextViews 感到困惑 https developer android com guide topics ui look and feel a