gpt4 book ai didi

android - 自定义 ScrollingViewBehavior 不加载

转载 作者:搜寻专家 更新时间:2023-11-01 09:37:20 25 4
gpt4 key购买 nike

我写了一个自定义的ScrollingViewBahavior:

package app.library.util.view;

//***

public class FillParentViewBehavior extends AppBarLayout.ScrollingViewBehavior {

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
View dependency) {
super.onDependentViewChanged(parent, child, dependency);

resizeChildAsNeeded(child, dependency);

return false;
}
}

接下来,我在资源中声明了一个类路径(类比appcompat声明):

<resources>
<string name="appbar_fill_parent_view_behavior" translatable="false">app.library.util.view.FillParentViewBehavior</string>
</resources>

并将其设置在我的布局中:

<layout
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.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.library.activity.MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Inner views -->

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

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_fill_parent_view_behavior"/>

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

但是我的应用程序崩溃了,日志输出:

java.lang.RuntimeException: Unable to start activity ComponentInfo{app/app.library.activity.MainActivity}: java.lang.RuntimeException: Could not inflate Behavior subclass app.library.util.view.FillParentViewBehavior

我试图重建一个项目,但它没有帮助我。这很奇怪,因为来自 AppCompat 库的行为完美无缺。
我做错了什么?

最佳答案

因为您正试图从代码创建此类,所以您需要再添加一个构造函数(通常,对于您从代码创建的任何 XML View 都是如此)。

所以,你的类应该是这样的:

public class FillParentViewBehavior extends AppBarLayout.ScrollingViewBehavior {

public FillParentViewBehavior() {
super();
}

public FillParentViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
View dependency) {
super.onDependentViewChanged(parent, child, dependency);

resizeChildAsNeeded(child, dependency);

return false;
}
}

关于android - 自定义 ScrollingViewBehavior 不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42050656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com