【问题标题】:Second toolbar's scrollFlag attribute is applied by the first toolbar第二个工具栏的 scrollFlag 属性由第一个工具栏应用
【发布时间】:2018-06-26 21:59:10
【问题描述】:

场景:

当其中一个toolbars 消失时,第二个toolbar 将适应第一个toolbarscrollFlag,而不是它自己的scrollFlag

注意:

只有当toolbars 之一是gone 时才会发生奇怪的行为, 如果它们都是visible,则scrollFlag 属性被正确设置为每个工具栏。

示例 1:

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_edit_toolbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/accent"
        app:contentInsetStart="@dimen/content_inset"
        app:navigationIcon="@drawable/ic_clear"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/white"
        tools:ignore="UnusedAttribute" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_normal_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:contentInsetStart="@dimen/content_inset"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/app_name"
        tools:ignore="UnusedAttribute" />

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

在这种情况下运行应用程序时,当显示toolbars 之一时,它的scrollFlag 行为与appbar_edit_toolbar 相同(即:滚动时始终显示的默认行为),而不是@显示 987654334@,其 scrollFlag 行为应为 scroll|enterAlways,如 xml 中所示。

示例 2:

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_normal_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:contentInsetStart="@dimen/content_inset"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/app_name"
        tools:ignore="UnusedAttribute" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_edit_toolbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/accent"
        app:contentInsetStart="@dimen/content_inset"
        app:navigationIcon="@drawable/ic_clear"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/white"
        tools:ignore="UnusedAttribute" />

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

在这种情况下运行应用程序时,当显示toolbars 之一时,它的scrollFlag 行为与appbar_normal_toolbar 中的一样(即:scroll|enterAlways),而不是在显示appbar_edit_toolbar 时,它的scrollFlag 行为应该是默认的(即始终显示)。

我尝试动态设置scrollFlags 属性,使AppBarLayout 无效,但这不起作用。

我很确定这种行为的发生是由于 Android 库中的一个错误,因为他们不希望我消失其中一个工具栏。

你们怎么看?有什么其他方法可以让我的 Activity 有 2 个不同的工具栏,每次只能看到其中一个?

【问题讨论】:

  • 为什么在同一页面中需要两个工具栏。 !!
  • @ragu-swaminatahan 我的活动处理激活第一个工具栏或第二个工具栏的“模式”标志。他们每个人都有不同的外观和菜单来膨胀。
  • 您可以使用单个工具栏并在代码中动态处理它们,这更简单
  • @idish 你的代码有什么问题。对我来说,滚动标志的正确行为
  • @RaguSwaminathan 使用单个工具栏并不是非常“设计”的解决方案,正如我所说,每个工具栏都代表我的活动的不同状态,并且它们具有不同的外观和菜单项。跨度>

标签: android scroll visibility android-toolbar android-appbarlayout


【解决方案1】:

对我来说工作正常。检查这个相同的代码。

<?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"
tools:context="md.com.androidui.MutipleToolbar">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:title="Scrolled Toolbar"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorAccent"
        app:title="Fixed Toolbar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</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">

    <include layout="@layout/content_mutiple_toolbar" />

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

输出 案例 1:两者都可见

案例 2:滚动工具栏仅可见

案例 3:固定工具栏仅可见

【讨论】:

  • 您是否有机会将您的整个项目上传到任何地方?所以我可以直接测试吗?干杯!
  • 我没有在那个项目中做更多的事情。那是示例创建的项目。我只是制作新项目,然后用它进行测试。
猜你喜欢
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多