【问题标题】:Toolbar does not Hide/Shown on Scrolling in CoordinatorLayout在 CoordinatorLayout 中滚动时工具栏不会隐藏/显示
【发布时间】:2017-06-28 19:05:05
【问题描述】:

我正在尝试自动隐藏和显示取决于在 ViewPager 中实现的 TextView 的滚动。但是,工具栏永远不会在滚动时隐藏。 主要 XML activity_data_display.xml

<android.support.v4.widget.DrawerLayout 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:id="@+id/activity_drawer"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.regio.developers.upasana.DataDisplayActivity">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/activity_data_display"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">

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

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

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

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

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

这是在 ViewPager 中膨胀的布局 data_swipe_layout.xml

<android.support.v4.widget.NestedScrollView
 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_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dataviewpager_textview"
        android:textColor="#FFF"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:paddingRight="35dp"
        android:paddingLeft="35dp"
        android:textSize="20sp"
        android:gravity="center"/>

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

添加 工具栏布局文件 toolbar_layout.xml

<?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="wrap_content"
 android:background="?attr/colorPrimary"
 android:fitsSystemWindows="true"
 android:minHeight="?attr/actionBarSize"
 app:theme="@style/ToolBarStyle"
 app:layout_scrollFlags="scroll|enterAlways"
>

</android.support.v7.widget.Toolbar>

请解决我的问题?

我尝试了不同的方法来使它像在 Main XML 文件中使用 NestedScrollView 一样工作。但是,它只是没有帮助。

请尝试自己实施,然后给我解决方案。

请记住,我使用过包含数百行文本的 TextView。

另外,如何为ViewPager中的TextView实现可手动拖动的ScrollBar?

【问题讨论】:

  • 在工具栏中,你必须使用 app:layout_scrollFlags.check this link
  • 我在 toolbar_layout 文件中使用了 app:layout_scrollFlags="scroll|enterAlways"

标签: android xml layout scrollview


【解决方案1】:

您正在使用包含的工具栏布局,您应该将此属性设置为 app:layout_scrollFlags="scroll|enterAlways"

对于 AppBarLayout 或在 AppBarLayout 中编写工具栏的代码。

希望这会有所帮助。

【讨论】:

  • 谢谢它的工作。另外你能告诉我如何显示滚动条吗?
  • 把它全部放在一个 或者你可以将布局的属性设置为可滚动。希望这适用于您的情况
【解决方案2】:
Check this.
 it is working
<android.support.v4.widget.DrawerLayout 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:id="@+id/activity_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarsdfs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        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_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/dataviewpager_textview"
            android:textColor="#FFF"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:paddingRight="35dp"
            android:paddingLeft="35dp"
            android:textSize="20sp"
            android:text=""
            android:gravity="center"/>

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

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

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多