如何在Android API中使用矢量drawables 21?

iam*_*181 63 java android vector android-layout

我正在开发一个Android项目,我选择<vector>显示图标,因为它具有适应性和动态性,但是,我只能在运行Android的设备上运行此应用程序,该设备具有API 21或更高版本.我的问题是如何<vector>在较低的Android版本即API 14或类型上使用.谢谢!

<!-- drawable/ic_android_debug_bridge.xmlxml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="48dp"
android:width="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@color/primaryColorDark"
    android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 `0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" /></vector>`
Run Code Online (Sandbox Code Playgroud)

Ale*_*jak 82

使用支持库23.2,Vector Drawables的真正支持一直提供给API v7.建议通过添加禁用以前版本的支持,该支持在构建期间呈现PNG

// Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }
Run Code Online (Sandbox Code Playgroud)

build.gradle文件.

实施相当简单.Drawables上有一个新的srcCompat属性:

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:srcCompat="@drawable/ic_add" />    <= this is new
Run Code Online (Sandbox Code Playgroud)

在TextView的drawableLeft属性中也支持Vector Drawables .

来源:图书馆公告

但是,我仍然会推荐像Iconics库,AndroidSVG或其他字体图标或SVG解决方案,以获得完整的SVG-standand支持.

  • 你是如何以编程方式执行此操作的? (3认同)

Dav*_*and 81

通过支持库支持前Lollipop的VectorDrawable,但使用它们的方式取决于您拥有的支持库的版本.它可能不适用于所有情况.

我已经使这个图表有所帮助(对支持库23.4.0有效 - 至少 - 25.1.0).

VectorDrawable cheatsheet

  • 这是有史以来最好的答案. (3认同)

Yaz*_*006 39

我找到了解决方案 对于那些使用TextView和其他"android"命名空间属性搜索解决方案的人.首先,这是必要的:

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }
Run Code Online (Sandbox Code Playgroud)

在应用程序类中定义:

    @Override
    public void onCreate() {
        super.onCreate();
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
Run Code Online (Sandbox Code Playgroud)

现在你可以使用app:srcCompat="@drawable/ic_add",但如果您尝试使用android:background=android:drawableLeft=将崩溃的应用程序"错误充气"异常.

我们可以ic_add_wrapped.xml为这个向量创建包装drawable :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_add"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

现在它可以用于任何属性,如drawableLeft或background.刚设置android:drawableLeft="@drawable/ic_add_wrapped.xml".警告!这是一个解决方案.因此,您自己承担风险.


2De*_*Dee 26

Vector Drawables现在向后兼容,只需将gradle版本升级到1.4.0-beta3或更高版本,然后升级IDE:

我们也很高兴为Android Studio 1.4中的矢量资产提供向后兼容性.在res/drawable中有一个vectorDrawable图像后,Gradle插件会在构建时间内自动生成API级别20及以下的栅格PNG图像.这意味着您只需为应用项目更新和维护矢量资产,Android Studio就可以处理图像转换过程.

http://android-developers.blogspot.com.uy/2015/09/android-studio-14.html

  • 这不完全正确.它仅适用于imageView,其余部分将在旧的Android版本上崩溃. (41认同)
  • @RedM是的,但仅限于代码(Java/kotlin).不是XML(任何类型),因为它可能会导致崩溃.要通过代码获取支持库的VectorDrawable,请使用"AppCompatResources.getDrawable".不要使用`ResourcesCompat`或`ContextCompat`.请在此处阅读:/sf/answers/3376594091/ (2认同)

meh*_*avi 20

你需要使用android Support Repository 30+如果你使用android studio并且如果使用Eclipse需要android支持库23.2.1+.

检查你的build.gradle(项目)是否使用版本2.0+在你的build.gradle(app)中添加以下代码

// Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }
Run Code Online (Sandbox Code Playgroud)

并且:如果在您的build.gradle(应用程序)中使用1.5版以下

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }  
Run Code Online (Sandbox Code Playgroud)

这是使用矢量图标的示例代码:

<ImageView  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add"
  tools:ignore="MissingPrefix" />
Run Code Online (Sandbox Code Playgroud)

要么

<ImageButton
  android:layout_width="wrap_content"
  android:background="@android:color/transparent"
  app:srcCompat="@drawable/camera"
  tools:ignore="MissingPrefix"
  android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

在TextView的drawableLeft属性中也支持Vector Drawables .但它对我来说有效,但我仍然不知道它如何适用于低api.

如果您想在API 21下兼容,请记住:

  • 你不能android:background在xml或View.setBackgroundResource()函数中使用该属性.你需要使用View.setBackground().
  • 你不能在StateListDrawable xml-s或其他xml drawables中使用svg-s,你必须以编程方式生成它们.
  • 在通知的情况下你不能使用svg-s.

  • 对于TextView,它会崩溃. (3认同)

cVo*_*nin 16

当您需要以编程方式添加VectorDrawable(从SVG创建)时,您可以这样做:

icon = VectorDrawableCompat.create(resources, R.drawable.ic_map_black_24dp, null)
Run Code Online (Sandbox Code Playgroud)


小智 5

为了与较低版本兼容,

  1. 在gradle中添加以下内容,

    android {  
       defaultConfig {  
         vectorDrawables.useSupportLibrary = true  
        }  
     }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您的应用程序类的onCreate()中添加以下代码,

    @Override
        public void onCreate() {
            super.onCreate();
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在用于imageView的xml中,

    <ImageView
        android:id="@+id/imageViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        app:srcCompat="@drawable/ic_success"/>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 如果您要以编程方式更改图片来源,请使用此选项,

    imageView.setImageResource(R.drawable.ic_launcher);
    
    Run Code Online (Sandbox Code Playgroud)