首页 > 解决方案 > 不在顶部时将工具栏设置为透明?

问题描述

我目前有一个带有工具栏的片段,该片段在向下滚动时“消失”并在向上滚动时“重新出现”,但我想更改它,以便工具栏的背景在显示时是透明的,而不是一直在顶部(或甚至总是透明的)。这是我目前拥有的:

活动片段.xml:

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

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/CustomActionBar" />

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

我在 Toolbar 和 AppBarLayout 上都试过android:background="@null"了,但是没有用。

我怀疑问题出在我如何/在哪里膨胀显示在下面的recylerview fragment_container

FragmentAlbumGallery.java:

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_recycler_gallery, container, false);
        mAlbumRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        mAlbumRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
return v;
}

fragment_recycler_gallery.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityGallery"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>

我的片段现在的截图: https://imgur.com/dRsCOar

标签: androidandroid-fragmentsandroid-toolbar

解决方案


您需要自定义样式CustomActionBar

<style name="CustomActionBar">
    <item name="android:background">@android:color/transparent</item>
</style>

就是这样,伙计们


推荐阅读