【问题标题】:Error when Hiding toolbar with AppCompat-v7使用 AppCompat-v7 隐藏工具栏时出错
【发布时间】:2015-05-05 09:11:41
【问题描述】:

我试图从文本更改侦听器中隐藏工具栏

public void onTextChanged(CharSequence s, int start, int before, int count) {
    toolbar.animate()
        .translationY(-toolbar.getBottom())
        .setInterpolator(new AccelerateInterpolator())
        .start();
}

此解决方案在更高级别的 api 上完美运行(成功在 kitkat 上运行)。但是在运行 api 级别 10 的设备时出现以下错误。

java.lang.NoSuchMethodError: android.support.v7.widget.Toolbar.animate

布局xml部分是

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="#ff6d7fe2"
    app:contentInsetEnd="0dp">
</android.support.v7.widget.Toolbar>

我没有将工具栏设置为支持操作栏。为什么即使使用 android.support.v7.widget.Toolbar 也会出现此错误?

【问题讨论】:

    标签: android android-support-library android-toolbar


    【解决方案1】:

    这个错误是因为你使用ViewPropertyAnimator方法toolbar.animate()。而且这个只兼容12以上的API。所以可以使用JakeWharton的NineOldAndroid库使其兼容12以下的API。

    【讨论】:

    • 我的项目已经包含了nineoldandroids-2.4.0.jar 用于滑动抽屉。相同的lib是否在上述情况下生效?
    • 检查您班级中的导入。是来自 Nineoldandroid 还是来自 android 的
    • 确保您使用的是来自库的toolbar.animate(),而不是本机
    【解决方案2】:

    Toolbar 只是一个视图,animate 是在 v10 之后引入的 View 上的一个方法 - 因此它不存在并且当您尝试在 v10 设备上调用它时会崩溃。

    你可以使用

    ViewCompat.animate(toolbar).translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
    

    但这不会在 ICS 之前做任何动画。您可以按照 Sajal 的建议使用 NineOldAndroid,将动画放到旧设备上,或者使用旧动画框架为视图设置动画。

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 2014-12-25
      • 1970-01-01
      • 2015-03-10
      • 2015-01-07
      • 2015-01-29
      • 2015-04-19
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多