AppCompat 23.3 Support Vectors no longer work?

前端 未结 3 763
日久生厌
日久生厌 2020-12-02 09:00

I was using the support vector drawables added in Support Library 23.2 along with AppCompat. I was using vector drawables both with app:srcCompat and inside a <

相关标签:
3条回答
  • 2020-12-02 09:32

    Update: They enable it again in Support Library 23:

    For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

    Check this link: 23.4.0 available now
    -----------------------------------------------------------

    As per the release announcement for Android Support Library 23.3:

    For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1 [ https://code.google.com/p/android/issues/detail?id=205236, https://code.google.com/p/android/issues/detail?id=204708 ]. Using app:srcCompat and setImageResource() continues to work.

    So this is an expected behavior change. You'll have to use non-vector graphics for any case not handled by srcCompat.

    If you'd like to continue to use vectors prior to API 21, you can remove the line

    vectorDrawables.useSupportLibrary = true
    

    (or the equivalent if you using the 1.5 Gradle plugin as shown in the 23.2 blog post).

    This will cause Android Studio to generate PNGs at compile time for apps with a minSdkVersion less than API 21 while using your vectors on API 21+ devices, allowing you to keep the same code as with 23.2.1 at the cost of additional APK size.

    0 讨论(0)
  • 2020-12-02 09:35

    To use vectors as compoundDrawables ( ex. for textview ) without using

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    

    which leads to documented high memory usage, just inflate the vector using

    Drawable drawable = AppCompatResources.getDrawable( getContext(), R.drawable.vector_resID );
    if( drawable != null ) drawable.setBounds( 0, 0, iconSize, iconSize );
    TextViewCompat.setCompoundDrawablesRelative( textView, null, null, drawable, null);
    

    This is the way the navDrawer works

    0 讨论(0)
  • 2020-12-02 09:36

    VectorDrawable support for pre-Lollipop was added in Support Library 23.2.0, then partially removed in 23.3.0. In 23.4.0 and above (at least 25.1.0), that removed part is back but behind an optional flag (because it comes with a price).

    To sum up: in Support Library 23.4.0 to at least 25.1.0, you can get VectorDrawable to work in some cases.

    I have made this diagram to help.

    0 讨论(0)
提交回复
热议问题