在膨胀视图期间崩溃与传染媒介drawable色彩颜色选择器

时间:2016-07-30 11:05:31

标签: android selector android-vectordrawable

我想使用带有色调选择器的矢量drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/gray_ef" android:state_enabled="false"/>
    <item android:color="@color/gray_ef" android:state_pressed="true"/>
    <item android:color="@color/app_main_blue"/>
</selector>

风格声明:

 <style name="SocialButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:adjustViewBounds">true</item>
        <item name="android:background">@drawable/into_social_bg</item>
        <item name="android:tint">@color/intro_social_tiny</item>
        <item name="android:clickable">true</item>
        <item name="android:layout_marginLeft">1dp</item>
        <item name="android:layout_marginRight">1dp</item>
    </style>

和布局xml中的声明

<ImageView
            android:id="@+id/tw_ImageView_AddSocialFragment"
            style="@style/SocialButton"
            app:srcCompat="@drawable/v_soc_tw"/>

它在设备API 21+上运行良好,但在较低的时候我有一个错误:

android.view.InflateException: Binary XML file line #15: Error inflating class ImageView
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                         at com.cs.liker.ui.intro.AddSocialFragment.onCreateView(AddSocialFragment.java:56)
                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                         at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                         at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164)
                                                         at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                         at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1019)
                                                         at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2978)
                                                         at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:279)
                                                         at com.cs.liker.ui.intro.IntroActivity.checkViewPagerCount(IntroActivity.java:79)
                                                         at com.cs.liker.ui.intro.IntroActivity.moveNextScreen(IntroActivity.java:66)
                                                         at com.cs.liker.ui.intro.LicenseFragment.click(LicenseFragment.java:34)
                                                         at com.cs.liker.ui.intro.LicenseFragment$$ViewBinder$1.doClick(LicenseFragment$$ViewBinder.java:17)
                                                         at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
                                                         at android.view.View.performClick(View.java:4463)
                                                         at android.view.View$PerformClick.run(View.java:18770)
                                                         at android.os.Handler.handleCallback(Handler.java:808)
                                                         at android.os.Handler.dispatchMessage(Handler.java:103)
                                                         at android.os.Looper.loop(Looper.java:193)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                         at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NumberFormatException: Invalid int: "res/color/intro_social_tiny.xml"
                                                         at java.lang.Integer.invalidInt(Integer.java:137)
                                                         at java.lang.Integer.parse(Integer.java:374)
                                                         at java.lang.Integer.parseInt(Integer.java:365)
                                                         at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
                                                         at android.content.res.TypedArray.getInt(TypedArray.java:255)
                                                         at android.widget.ImageView.<init>(ImageView.java:155)
                                                         at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                         at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                         at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                         at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963)
                                                         at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1022)
                                                         at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                         at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172)
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                         at com.cs.liker.ui.intro.AddSocialFragment.onCreateView(AddSocialFragment.java:56) 
                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
                                                         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
                                                         at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                         at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164) 
                                                         at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) 
                                                         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608) 
                                                         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570) 
                                                         at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1019) 
                                                         at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2978) 
                                                         at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:279) 
                                                         at com.cs.liker.ui.intro.IntroActivity.checkViewPagerCount(IntroActivity.java:79) 
                                                         at com.cs.liker.ui.intro.IntroActivity.moveNextScreen(IntroActivity.java:66) 
                                                         at com.cs.liker.ui.intro.LicenseFragment.click(LicenseFragment.java:34) 
                                                         at com.cs.liker.ui.intro.LicenseFragment$$ViewBinder$1.doClick(LicenseFragment$$ViewBinder.java:17) 
                                                         at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22) 
                                                         at android.view.View.performClick(View.java:4463) 
                                                         at android.view.View$PerformClick.run(View.java:18770) 
                                                         at android.os.Handler.handleCallback(Handler.java:808) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:103) 
                                                         at android.os.Looper.loop(Looper.java:193) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                                                         at dalvik.system.NativeStart.main(Native Method) 

我是矢量drawable的新手,有人可以给我建议锄头来解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

<强>布局

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/tw_ImageView_AddSocialFragment"
    style="@style/SocialButton"
    app:srcCompat="@drawable/v_soc_tw" />

<强>风格

<style name="SocialButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:adjustViewBounds">true</item>
        <item name="android:background">@drawable/into_social_bg</item>
        <item name="android:clickable">true</item>
        <item name="android:layout_marginLeft">1dp</item>
        <item name="android:layout_marginRight">1dp</item>
 </style>

<强>颜色

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/gray_ef" android:state_enabled="false"/>
    <item android:color="@color/gray_ef" android:state_pressed="true"/>
    <item android:color="@color/app_main_blue"/>
</selector>

<强>矢量

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:tint="@color/intro_social_tiny">
    <path
        android:fillColor="#616161"
        android:pathData="M17,12h-5v5h5v-5zM16,1v2L8,3L8,1L6,1v2L5,3c-1.11,0 -1.99,0.9 -1.99,2L3,19c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2h-1L18,1h-2zM19,19L5,19L5,8h14v11z"/>
</vector>

from the official docs of VectorDrawable

  

机器人:色彩       作为色调应用于drawable的颜色。默认情况下,不应用任何色调。

答案 1 :(得分:0)

如果您想使用带有VectorDrawables的视图选择器,您需要添加:

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

到您希望在Android 5以下版本的设备上使用VectorDrawables的每个活动。