滚动时工具栏不能折叠
问题描述:
我想在我的android应用程序中实现折叠工具栏。我能够按照我想要的方式显示工具栏,但当滚动时它不会折叠。滚动时工具栏不能折叠
我使用下面的代码
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/main_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.gigamole.navigationtabstrip.NavigationTabStrip
android:id="@+id/nts_strip"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:nts_active_color="@color/white"
app:nts_animation_duration="300"
app:nts_color="@color/white"
app:nts_corners_radius="1.5dp"
app:nts_inactive_color="@color/white_transparent"
app:nts_titles="@array/nts_titles" />
<android.support.v4.view.ViewPager
android:id="@+id/pager_photos"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white_transparent" />
</LinearLayout>
</LinearLayout>
main_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/tv_toolbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
下面是屏幕看起来的样子
答
为LinearLayout
添加
app:layout_behavior="@string/appbar_scrolling_view_behavior"
在activity.xml
文件。
另外补充
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
为Toolbar
答
必须在工具栏
https://developer.android.com/reference/android/support/design/widget/AppBarLayout.LayoutParams.html
样品
中添加滚动标志 <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/>
+0
仍然不起作用。 –
答
试试这个
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<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="custom.com.myapplication.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<include layout="@layout/main_toolbar" />
</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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.gigamole.navigationtabstrip.NavigationTabStrip
android:id="@+id/nts_strip"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:nts_active_color="@color/white"
app:nts_animation_duration="300"
app:nts_color="@color/white"
app:nts_corners_radius="1.5dp"
app:nts_inactive_color="@color/white_transparent"
app:nts_titles="@array/nts_titles" />
<android.support.v4.view.ViewPager
android:id="@+id/pager_photos"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</android.support.v4.widget.NestedScrollView>
工具栏
<?xml version="1.0" encoding="utf-8"?>
<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="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/tv_toolbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
+0
@manlevj:我早些时候试过这个问题,因为viewpager里面有recyclerview。应用程序冻结了用户界面,并且滚动不再有效。 –
没有变化,仍然没有工作 –
变化'应用:layout_scrollFlags = “exitUntilCollapsed”''到应用程序:layout_scrollFlags = “滚动| enterAlwaysCollapsed”''为CollapsingToolbarLayout' @KaranMer – Piyush
仍然没有工作。 –