Android:App尝试获取AppBarLayout LayoutParams时崩溃

时间:2017-02-15 13:37:07

标签: android android-toolbar android-collapsingtoolbarlayout android-appbarlayout

我在尝试设置AppBarLayout参数时遇到ClassCastException。下面是我的XML代码

<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.testapp.ConsoleActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:id="@+id/app_bar_layout">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:id="@+id/app_bar_account_cards_pager"
                app:layout_collapseMode="parallax"
                android:layout_marginTop="?attr/actionBarSize" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode="pin" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:id="@+id/nested_scroll_bar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/content_console"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>

    </android.support.v4.widget.NestedScrollView>



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

我正在尝试使用以下java代码获取AppBarLayoutParams的实例

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
p.setScrollFlags(0);
toolbar.setLayoutParams(p);

但我正在使用此行java.lang.ClassCastException: android.support.design.widget.CollapsingToolbarLayout$LayoutParams cannot be cast to android.support.design.widget.AppBarLayout$LayoutParams获取AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

你必须使用

进行投射
android.support.design.widget.CollapsingToolbarLayout.LayoutParams

而不是

android.support.design.widget.AppBarLayout.LayoutParams

编辑:布局参数由视图的移民父母提供

CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
p.setScrollFlags(0);
collapsingToolbar.setLayoutParams(p);