Support libraries to add for Autosizing TextViews

2019-06-27 04:18发布

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.

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

Edit: 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"/>

Edit: My Solution for now.

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"/>

4条回答
看我几分像从前
2楼-- · 2019-06-27 04:51

There are contradictory answers regarding whether to use:

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

or:

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

The difference is related to the LayoutInflater used. For example if you're using an AppCompatActivity then TextView is automatically replaced. But if you're using a FragmentActivity (which AppCompatActivity extends from), it will not, so you need to directly use AppCompatTextView to take advantage of the features.

In either case it should be app not android for pre-26 builds - but you can provide both in a single layout.

Also, you can use styles for both as well:

    <style name="TextDefault" parent="@android:style/TextAppearance.DeviceDefault">
        <item name="android:autoSizeTextType" tools:ignore="NewApi">uniform</item>
        <item name="autoSizeTextType">uniform</item>
    </style>

Note that the name does NOT include app for pre-26 builds when used in styles.

查看更多
Luminary・发光体
3楼-- · 2019-06-27 05:03

Change this:

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

to this:

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

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.

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).

查看更多
小情绪 Triste *
4楼-- · 2019-06-27 05:08

Try changing android: to app: namespace :

<android.support.v7.widget.AppCompatTextView
    app:autoSizeTextType="uniform"
    .../>
查看更多
Bombasti
5楼-- · 2019-06-27 05:14

Autosizing is in support library. Use AppCompatTextView instead of regular TextView.

查看更多
登录 后发表回答