支持库添加用于自动调整TextViews [英] Support libraries to add for Autosizing TextViews

查看:78
本文介绍了支持库添加用于自动调整TextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am confused about adding the correct support libraries in Android Studio to be able to use AutoSizing TextViews. https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html
The description of what support libraries to use is kind of vague on this page. I have tried to import the suggested libraries, but either I am requesting the wrong libraries (they are not found, such as the android.support.v4.widget package) or I am doing something else incorrectly.

我的最小SDK是21,最大SDK是27,因此,如果我导入了正确的库,则自动调整大小"功能应与我的应用程序向后兼容.但是,在屏幕设计视图中,我收到警告属性autoSizeTextType仅在API级别26和更高级别(当前最小值为21)中使用"

My min SDK is 21 and max SDK is 27, so the AutoSizing feature should be backwards compatible for my app if I have the correct libraries imported. However, in the screen design view, I get the warning "Attribute autoSizeTextType is only used in API level 26 and higher (current min is 21)"

据我所知,我具有正确的支持库.项目结构依存关系如下:

As far as I can tell, I have the correct support libraries included. Project Structure Dependencies are as follows:

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'

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

Any suggestions? I also could use some links on how to convert the suggested libraries to the actual libraries to be imported. Thanks

android:autoSizeTextType问题的部分布局列表.我收到列出的两个textview的警告消息.

partial list of layout with android:autoSizeTextType problem. I get the warning message for both of the textviews listed.

<?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是否正在调整大小,但这在我目前正在测试的设备上看起来不错.如建议的那样,使用了appSize前缀的app:前缀对纯TextView进行了尝试,但是收到了错误消息为标签TextView找到了意外的命名空间前缀"app"".当我将其更改为android.support.v7.widget.AppCompatTextView时,错误消失了.感谢您的帮助.

This layout is what I ended up with that seems to work for now. Not sure if the autoSizing is resizing or not, but this looks good on the devices I am testing for at this time. Tried the plain TextView using the app: prefix for autoSizeTextType as was suggested, but received the error "Unexpected namespace prefix "app" found for tag TextView". When I changed it to android.support.v7.widget.AppCompatTextView, the error went away. Thanks for the help.

<?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会自动注入AppCompat窗口小部件来代替标准窗口小部件.

There's no need to reference AppCompatTextView directly. The LayoutInflater used by the support library will automatically inject AppCompat widgets in place of standard widgets.

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

Additionally, to get auto-sizing working pre-API-26, you need to be using the support library implementation, which means you need to be using the AppCompat attributes (with the app: namespace) instead of the platform attributes (with the android: namespace).

这篇关于支持库添加用于自动调整TextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆