2016-07-23 33 views
4

好吧,我讀過最類似的問題,但沒有固定我的問題無效繪製標籤矢量 - 程序兼容性-V7:24.1.1

gradle這個classpath 'com.android.tools.build:gradle:2.1.2'

android { 
    defaultConfig { 
     minSdkVersion 16 
     targetSdkVersion 24 
     vectorDrawables.useSupportLibrary = true 
    } 
} 

dependencies { 
    compile 'com.android.support:recyclerview-v7:24.1.1' 
    compile 'com.android.support:appcompat-v7:24.1.1' 
    compile 'com.android.support:cardview-v7:24.1.1' 
    compile 'com.android.support:support-v4:24.1.1' 
    compile 'com.android.support:design:24.1.1' 
} 

activity.xml

提供答案
<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 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:id="@+id/ScrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".ViewItem" 
    tools:ignore="MissingPrefix"> 

    ... 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_gravity="center_vertical" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="10dp" 
     android:foregroundGravity="center_vertical|center|center_horizontal" 
     app:srcCompat="@drawable/ic_car" /> 

    ... 

</ScrollView> 

矢量ic_car.xml

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportHeight="24" 
    android:viewportWidth="24"> 

    <path 
     android:fillColor="#5c5c5c" 
     android:pathData="M18.92 6.01C18.72 5.42 18.16 5 17.5 5h-11c-.66 0-1.21 .42 -1.42 1.01L3 12v8c0 
.55 .45 1 1 1h1c.55 0 1-.45 1-1v-1h12v1c0 .55 .45 1 1 1h1c.55 0 1-.45 
1-1v-8l-2.08-5.99zM6.5 16c-.83 0-1.5-.67-1.5-1.5S5.67 13 6.5 13s1.5 .67 1.5 
1.5S7.33 16 6.5 16zm11 0c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5 .67 1.5 
1.5-.67 1.5-1.5 1.5zM5 11l1.5-4.5h11L19 11H5z" /> 
    <path android:pathData="M0 0h24v24H0z" /> 
</vector> 

我的活動分類延伸AppCompatActivity和所有android:src標記更改爲app:srcCompat但仍然看到Invalid drawable tag vector錯誤minSdkVersion它被設置爲16

+0

,您是否試圖使用'安卓src',而不是'應用:srcCompat'。 –

+0

@MuddassirAhmed是的,當然我做過 – PYPL

+0

只有在發行版本或所有版本中才會發生嗎? – lionscribe

回答

0

問題得到更新到Android Studio 2.2中

1

事實上,看着你的代碼,它應該可以在任何版本的庫中工作,因爲你只使用srcCompat屬性。
我懷疑的一個問題是,您的矢量是在drawable-21文件夾中,而在常規的Drawable文件夾中,您有單個文件引用這些Drawable。這是在庫的早期版本中完成的一種方法,但由於下面的更改不再有效。只需將實際的矢量文件移動到Drawable文件夾即可。
我們有時會遇到類似的問題,通過將ImageView更改爲AppCompatImageView來解決。儘管圖書館應該自動執行此操作,但似乎並非如此。
如果您的矢量是複合圖像的一部分,例如分層圖像,代碼仍然可能中斷。或者它在其他地方沒有發佈代碼,例如,如果您將矢量設置爲ImageButton上的左圖像。 @Zeke回答說,這些功能已在23.3中刪除。儘管如此,在23.4歸還,可製成通過添加以下到您的活動

static { 
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 
} 

他們仍然提醒你它可能會導致內存泄漏。請參閱https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.s48hqaw1u,儘管最小限度使用它不應該是一個問題。

一個簡單的解決方法是創建Drawable並將其設置在代碼中。像這樣的東西;

Drawable icon = AppCompatDrawableManager.get().getDrawable(context, resVectorID); 
// Set it for example as a left image of button 
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 

我還沒有測試過這個,並且沒有任何想法,如果這會導致內存問題或沒有。如果有人可以跟進,會喜歡它。

+0

需要做3件事; 1)'gradle'上的'vectorDrawables.useSupportLibrary = true'; 2)'static {AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); }關於Activity渲染SVG; 3)'App:srcCompat'而不是'ImageView'中的'android:src';即使錯過其中一個_will_ **崩潰**您的應用程序 - 從「24.2.0」開始 –

2

在我的情況後問題本身,事故發生因使用Resources.getDrawable(int)得到的棒棒糖預設備的矢量繪製。我試圖創建一個可繪製的文件傳入 ImageView.setImageDrawable(Drawable)

Google Blog Post for Android Support Library 23.2:

就Android支持庫23.3.0的,支持向量可繪可以 只能通過應用加載:srcCompat或setImageResource()。

解決方案:使用VectorDrawableCompat.create(Resources, int, Theme)來創建drawable。

您也可以使用ImageView.setImageResource(int)但是這會讀取/解碼UI線程上的位圖。

0

我不喜歡這樣的:

public static final Drawable getMyDrawable(Context context, int id) { 
    final int version = Build.VERSION.SDK_INT; 
    if (version >= 21) { 
     return ContextCompat.getDrawable(context, id); 
    } else { 
     return VectorDrawableCompat.create(context.getResources(), id, context.getTheme()); 
    } 
} 
相關問題