【问题标题】:How to properly use backwards compatible Vector Drawable with the latest Android Support Library?如何通过最新的 Android 支持库正确使用向后兼容的 Vector Drawable?
【发布时间】:2017-04-02 09:57:18
【问题描述】:

Vector drawable 不久前被添​​加到支持库中,从那时起 API 发生了很多变化:Gradle 标志、初始化程序块、选择器、自定义 XML 属性等。问题是 - 如何正确使用现在(支持 lib v25)在这些情况下:

  • 图像视图
  • TextView 可绘制
  • 菜单图标
  • 通知图标

XML 和编程方式。

【问题讨论】:

    标签: java android android-appcompat android-vectordrawable


    【解决方案1】:

    将最新的支持库添加到您应用的 build.gradle 依赖项中:

    compile 'com.android.support:appcompat-v7:26.0.2'
    

    并在同一文件中添加以下行:

    android {
        ...
        defaultConfig {
            ...
            vectorDrawables.useSupportLibrary = true
        }
        ...
    }
    

    通过Vector Asset Studio导入矢量图。

    就是这样,你准备好了!


    图像视图

    XML

    使用app:srcCompat 属性而不是android:src

    <ImageView
        ...
        app:srcCompat="@drawable/your_vector" 
        ... />
    

    以编程方式

    直接来自资源 id:

    imageView.setImageResource(R.drawable.your_drawable);
    

    设置为Drawable 对象(例如用于着色):

    Drawable vectorDrawable 
                    = AppCompatResources.getDrawable(context, R.drawable.your_vector);
    imageView.setImageDrawable(vectorDrawable);
    

    如果你想设置色调:

    DrawableCompat.setTint
                 (vectorDrawable, ContextCompat.getColor(context, R.color.your_color));
    

    TextView 可绘制

    XML

    没有简单的解决方案:XML 属性android:drawableTop(Bottom etc) 无法处理pre-Lollipop 上的矢量图。一种解决方案是add initializer block to activity and wrap vector into another XML drawable。第二个 - to define a custom TextView

    以编程方式

    直接设置资源是不行的,你必须使用Drawable对象。以与ImageView 相同的方式获取并使用适当的方法进行设置:

    textView.setCompoundDrawablesWithIntrinsicBounds(vectorDrawable, null, null, null);
    

    菜单图标

    没有什么特别的:

    <item
        ...
        android:icon="@drawable/your_vector"
        ... />
    
    menuItem.setIcon(R.drawable.your_vector);
    

    通知:

    它是impossible,你必须使用 PNG :(

    【讨论】:

    • 这两种情况都1+。
    • AppCompatResources.getDrawable 效果很好,谢谢。有人能告诉我为什么ContextCompat.getDrawable 不能使用完全相同的代码并在使用向量时给我一个错误吗?
    【解决方案2】:

    您需要将 vectorDrawables.useSupportLibrary = true 添加到您的 build.gradle 文件中:

    // Gradle Plugin 2.0+  
     android {  
       defaultConfig {  
         vectorDrawables.useSupportLibrary = true  
        }  
     } 
    

    您会注意到这个新属性仅存在于 Gradle 插件的 2.0 版本中。如果您使用的是 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"  
      }  
     }  
    

    您需要将 srcCompat 添加到您的 ImageView:

    <ImageView  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      app:srcCompat="@drawable/ic_add" /> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-05
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2010-09-21
      • 2011-02-09
      • 2019-12-22
      相关资源
      最近更新 更多