Android:尝试获取 AppBarLayout LayoutParams 时应用崩溃
Android: App crash while trying to get AppBarLayout LayoutParams
我在尝试设置 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();
。我该如何解决这个问题?
您必须使用
进行投射
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);
我在尝试设置 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();
。我该如何解决这个问题?
您必须使用
进行投射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);