因为Android 5.0 +上的浮动操作按钮上的样式attr而导致InflateException

时间:2017-03-02 09:22:27

标签: android floating-action-button

我正在重复此项目https://github.com/sitepoint-editors/FloatingActionButton_Animation_Project,并使用标准Floating Action Button

main.xml 布局中:

...
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/calls_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="64dp"
        android:src="@drawable/ic_plus"
        app:layout_anchor="@id/fragment_calls_swipeRefreshLayout"
        app:layout_anchorGravity="bottom|end"
        app:layout_behavior="com.my.utils.ScrollAwareFABBehavior"
        />

    <include layout="@layout/fab_add_layout" />
...

其他按钮 fab_add_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_group_add_black_24dp"
    style="@style/MyAppTheme.SubFab"
    app:fabSize="mini"
    />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_accounts"
    style="@style/MyAppTheme.SubFab"
    app:fabSize="mini"
    />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_opportunities"
    style="@style/MyAppTheme.SubFab"
    app:fabSize="mini"
    />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_leads"
    style="@style/MyAppTheme.SubFab"
    app:fabSize="mini"
    />
</FrameLayout>

在使用Android 4.4的设备上一切正常,但是从Lollipop开始的更高版本我收到错误:

Process: com.myfabs , PID: 11441
android.view.InflateException: Binary XML file line #36: Binary XML file line #6: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:561)
at android.view.LayoutInflater.inflate(LayoutInflater.java:437)
at fi.lunni.mobileadvisor.ui.fragments.CallsListFragment_.onCreateView(CallsListFragment_.java:54)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
at android.view.View.measure(View.java:18948)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
at android.view.View.measure(View.java:18948)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2713)
at android.view.View.measure(View.java:18948)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2158)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1252)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1488)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6256)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
at android.view.Choreographer.doCallbacks(Choreographer.java:698)
at android.view.Choreographer.doFrame(Choreographer.java:633)

我拿走style属性后一切正常。这是我的style

<style name="MyAppTheme.SubFab">
    <item name="android:layout_marginRight">24dp</item>
    <item name="android:layout_marginEnd" tools:targetApi="17">24dp</item>
    <item name="android:layout_marginBottom">72dp</item>
    <item name="android:backgroundTint" tools:targetApi="21">@color/blue</item>
    <item name="android:visibility">invisible</item>
    <item name="android:layout_gravity">bottom|right|end</item>
</style>

问题:你能解释一下这种情况吗?为什么会这样?

1 个答案:

答案 0 :(得分:1)

为您的样式提供父级,并且不要在您的样式名称中使用.,它有precise meaning

此外,您应该使用backgroundTint而不是android:backgroundTint,因为FloatingActionButton有自己的backgroundTint属性(其行为与android:backgroundTint略有不同),兼容还有21级之前的API级别:

<style name="SubFab" parent="@style/Widget.Design.FloatingActionButton">
    <item name="android:layout_marginRight">@dimen/sub_fab_rightMargin</item>
    <item name="android:layout_marginEnd" tools:targetApi="17">@dimen/sub_fab_rightMargin</item>
    <item name="android:layout_marginBottom">@dimen/sub_fab_bottomMargin</item>
    <item name="android:visibility">invisible</item>
    <item name="android:layout_gravity">bottom|right|end</item>
    <item name="backgroundTint">@color/blue</item>
</style>