当在我的.xml布局中使用带有矢量drawable的app:srcCompat时,我无法在运行android 4.2.1的设备上运行代码。例如,我有一个像这样的FAB:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
...
android:tint="@color/colorWhite"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@drawable/ic_add"/>
其中 ic_add 是一个矢量drawable:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
这在Lollipop上工作正常,但在运行android 4.2.1的设备上崩溃:
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #14: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at info.samuilife.samuiplus.user.offers.OffersFragment.onCreateView(OffersFragment.kt:34)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2034)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1239)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1087)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1613)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:713)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1389)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:783)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
at android.view.View.measure(View.java:15604)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
没有app:srcCompat行,它可以正常工作。
在我的build.gradle中,我也有:
defaultConfig {
vectorDrawables.useSupportLibrary = true
...
}
...
dependencies {
...
compile "com.android.support:appcompat-v7:26.0.0-alpha1"
}
我在这里缺少什么?任何想法/想法将不胜感激!
解决方案: This回答对我有用。
答案 0 :(得分:1)
You probably forget to add the design support library. So, you should add the Design support library in your dependency by adding the following line:
dependencies {
...
compile "com.android.support:design:27.0.0"
//compile "com.android.support:appcompat-v7:26.0.0-alpha1"
}
You don't need to add the appcompat library because it's implicitly included in design support library. And please remember to never using an alpha version of library in your code.
Read more about design support library at Using the Design Support Library
UPDATE
Add the Vector Drawable Support with:
compile "com.android.support:support-vector-drawable:27.0.0"
答案 1 :(得分:0)
试试这个
Android Api等级19或以下不支持矢量图像,我得到了 这个解决方案
在此设置您的小部件ID并从中删除Vector Drawable 插件
private void setBackgrounds(){
Drawable drawableLeft = AppCompatResources.getDrawable(modelet.getContext(), R.drawable.ic_local_taxi_black_16dp);
modelet.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
}