Android 工具栏位于Appbar顶部-CoordinatorLayout
我一直在努力使用AppBarLayout/Android 工具栏位于Appbar顶部-CoordinatorLayout,android,android-toolbar,android-coordinatorlayout,android-appbarlayout,Android,Android Toolbar,Android Coordinatorlayout,Android Appbarlayout,我一直在努力使用AppBarLayout/工具栏/CoordinatorView。希望工具栏在滚动时向上滑动,在向下滚动时立即返回(与大多数阅读应用程序一样,g+也是一个示例)。然而,我得到的是: 正常: 一个小卷轴: <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.an
工具栏
/CoordinatorView
。希望工具栏
在滚动时向上滑动,在向下滚动时立即返回(与大多数阅读应用程序一样,g+也是一个示例)。然而,我得到的是:
正常:
一个小卷轴:
<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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="main.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/id_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:title="Hello World"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/main__activitycontent"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="main.MainActivity"
tools:showIn="@layout/main__activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text"/>
</android.support.v4.widget.NestedScrollView>
完整滚动:
<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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="main.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/id_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:title="Hello World"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/main__activitycontent"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="main.MainActivity"
tools:showIn="@layout/main__activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text"/>
</android.support.v4.widget.NestedScrollView>
这是我的代码:
<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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="main.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/id_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:title="Hello World"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/main__activitycontent"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="main.MainActivity"
tools:showIn="@layout/main__activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text"/>
</android.support.v4.widget.NestedScrollView>
我读了很多博客文章,但他们的配置似乎都不适合我。向上滚动时隐藏/向下滚动时显示工作正常,只有工具栏
在appbar上移动。检查Android视图层次结构时,我看到工具栏上的位于深蓝色应用程序条上方,它们一起向上移动
编辑1
从CoordinatorLayout
中删除FitsSystemWindows=true
会导致预期的行为,但无论CoordinatorLayout的背景颜色如何,appbar都会变为白色:
我的猜测是,行为实际上没有改变,发生的事情是,它不再覆盖appbar,所以工具栏不会覆盖它
编辑2
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>
问题在于:
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
您可以将其添加到您的折叠工具栏布局
enterAlwaysCollapsed
中,这对于折叠工具栏布局
而不是工具栏
是一个很好的做法
更新:我发现布局和ID中没有什么问题
例如,我不确定您是否看到了,但我会解释一下。第一件事是,Toolbar
的id:id\u Toolbar
,在onCreate
中是:
Toolbar toolbar = (Toolbar) findViewById(R.id.main__toolbar);
因此,只需将其更改为:
<android.support.v7.widget.Toolbar
android:id="@+id/main__toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
最后,在这里你可以看到我对这个问题的回答:
我们有:
更新:我没有使用:
android:fitsSystemWindows="true"
也在坐标布局中。正如您在这里看到的示例:
更新:
要解决白色工具栏问题,请在values-v21/styles.xml上设置:
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
@color/colorPrimaryDark
这将提供适当的行为和外观。尝试删除android:fitsystemwindows=“true”
如果我删除android:fitsystemwindows=“true”,则显示您的工具栏位于系统应用程序栏下,因为您设置了此选项。如果我删除android:fitsystemwindows=“true”,行为将按预期工作,但应用程序栏将变为白色,而不是彩色PrimaryDark。我已尝试更改Coordinator布局的背景颜色,但它仍然为白色。您是否在AppTheme下的styles.xml中定义了@color/color\u primary\u dark
?即使在布局上更改为#0000FF也没有任何作用,因此这不是声明问题。删除fitsSystemWindow使Coordinator布局从AppBar的正下方开始,最后一件事是共享使用此布局的活动代码。通过在Coordinator布局中删除fitsSystemWindow=false,我成功地获得了此状态。但是,状态栏没有着色,因为CoordinadorLayout从状态栏下方开始。我找到了一个解决办法,将android:windowBackground设置为primaryDarkColor。更新后,我再次看到了奶酪广场
,似乎他没有将其添加到协调布局
,但他将其添加到抽屉
,无论如何,我的答案是关于该标志的正确答案。我会接受你的答案,只需添加一些内容即可完成:set@color/colorPrimaryDarkGood,但有一件事,请检查此链接:默认情况下,android:statusBarColor
继承了android:colorPrimaryDark
的值,所以实际上你不需要它,如果你看到任何奇怪的东西,应该用真正的设备修复它,我这么说是因为我以为你在使用模拟器。这是我发现最奇怪的事情:它应该,但它不是。我后来检查了v21/styles.xml,发现statusBarColor被覆盖并被设置为透明(这是Studio自动生成的代码)