29

我是 android 新手,但我遇到了FloatingActionButton问题

我的自定义行为类:

public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
    private static final String TAG = "ScrollingFABBehavior";

    public ScrollingFABBehavior(Context context, AttributeSet attrs,
            Handler mHandler) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton child, View directTargetChild, View target,
            int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child,
                        directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton child, View target, int dxConsumed,
            int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
                dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() == View.GONE) {
            child.show();
        }
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton
            child, View target) {
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }
}

片段 XML:

...

<android.support.design.widget.FloatingActionButton
        android:id="@+id/share_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:contentDescription="@string/action_share"
        android:elevation="@dimen/fab_elevation"
        android:src="@drawable/ic_share"
        app:layout_behavior=".ScrollingFABBehavior"/>

</android.support.design.widget.CoordinatorLayout>

片段膨胀 xml 时的 RuntimeError:

07-14 08:52:43.904 30785-30785/com.example.xyzreader E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.xyzreader, PID: 30785
                                                                       android.view.InflateException: Binary XML file line #115: Could not inflate Behavior subclass com.example.xyzreader.ui.ScrollingFABBehavior
                                                                       Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass com.example.xyzreader.ui.ScrollingFABBehavior
                                                                           at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:615)
                                                                           at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2652)

ETC

怎么了?

4

9 回答 9

42

将以下两个构造函数添加到您的FooterBehavior:

public FooterBehavior() {
}

public FooterBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}
于 2017-09-04T03:15:01.850 回答
24

确保您使用的是绝对路径而不是自定义行为类的相对路径。

例如:

app:layout_behavior="net.company.myapp.view.behavior.TextViewBehavior"
于 2019-05-02T13:08:54.220 回答
14

如果您使用的是 AndroidX(Android 团队用于在 Jetpack 中开发、测试、打包、版本和发布库的开源项目),那么您需要更新 XML。

在此处找到您的元素并替换:

支持:

android.support.design.widget.FloatingActionButton

安卓X:

com.google.android.material.floatingactionbutton.FloatingActionButton
于 2019-04-01T10:09:34.930 回答
12

如果您在 Android X 中使用 BottomSheet,则必须使用

app:layout_behavior="@string/bottom_sheet_behavior"

如果没有,那么使用

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
于 2020-06-27T08:30:45.443 回答
9

如果您的应用具有与实际包结构不同的包 ID,请确保指定自定义行为类的完整路径:

<android.support.design.widget.FloatingActionButton
        ...
        app:layout_behavior="com.example.xyzreader.ScrollingFABBehavior"/>
于 2019-11-06T11:56:16.723 回答
4

解决了。更改app:layout_behavior=".ScrollingFABBehavior"/>app:layout_behavior=".ui.ScrollingFABBehavior"/>

于 2017-07-14T13:33:47.050 回答
4

如果您使用的是 androidX,那么您的根布局应该是:

<androidx.coordinatorlayout.widget.CoordinatorLayout

bottom_sheet 的布局应包括:

app:layout_behavior="@string/bottom_sheet_behavior"

否则,如果您不使用 androidx 或使用支持库,那么您的根布局应该是:

<android.support.design.widget.CoordinatorLayout

bottom_sheet 的布局应包括:

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
于 2020-06-30T11:00:30.597 回答
4

我遇到了类似的问题并最终出现在此页面上。

我正在使用 Android X 上的底页

当我点击移动到应用程序中的新片段并且应用程序因RuntimeExeception 崩溃时,我运行了我的应用程序并收到以下错误消息:“Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehaviour”

我的布局属性如下:

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/player_bg"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehaviour"
    app:behavior_hideable="true"
    app:behavior_peekHeight="70dp">

当我收到该错误消息时,我更改了一行,然后应用程序运行了。

如上所述,当我使用 Android X 时,我从:

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehaviour"

我将此行更改为:

app:layout_behavior="@string/bottom_sheet_behavior"

这一更改意味着代码对我有用,没有崩溃,并且新的 Fragment 按预期显示。

于 2020-08-23T11:51:38.860 回答
0

如果您使用的是 Androidx Jetpack 依赖项,那么

代替

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
于 2021-06-08T03:04:35.363 回答