【问题标题】:Fragment's Toolbar transparent over Activity's toolbar片段工具栏在 Activity 的工具栏上透明
【发布时间】:2015-08-23 01:40:54
【问题描述】:

我有一个我似乎无法弄清楚的特殊情况,我有预感它涉及CollapsingToolbarLayout

我有一个具有以下 XML 布局的活动:

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                app:layout_collapseMode="parallax"/>

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

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

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

    <LinearLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

        <android.support.design.widget.TabLayout />
        <!-- Two fragments inside ViewPager each containing a RecyclerView -->
        <android.support.v4.view.ViewPager />

    </LinearLayout>

    <!-- User Clicks a button and fragment is added to this framelayout -->
    <!-- This fragment overlays on top of the entire Activity layout -->
    <!-- This fragment also contains a recyclerview -->
    <FrameLayout/>


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

这个Activity 的工具栏看起来像这样:

然后,当我在 Activity 的布局之上打开 Fragment 时,会发生这种情况:

这是片段的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:elevation="4dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.v7.widget.Toolbar
        android:id="@+id/fragmentToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

</LinearLayout>

这就是我初始化Fragment的工具栏的方式:

private void initToolBar(View root) {
    Toolbar toolbar = (Toolbar) root.findViewById(R.id.fragmentToolbar);
    toolbar.setBackgroundColor(mColor);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(mTitle);
}

为什么会出现这个问题?

【问题讨论】:

  • 为什么不直接将 AppBarLayout 移动到片段的布局中呢?为什么有两个工具栏?假设您正在执行整个一项活动,多片段范式。
  • 因为我有一个viewpager,然后在Activity + ViewPager之上创建另一个fragment的activity上有一个按钮。
  • 知道了.. mColor 的值是多少?这是一个 int 值还是什么?
  • 是的,这可能是我做的第一件事。
  • 确保颜色值已经被解析,比如getResources().getColor(mColorInt);我什至无法描述为了弄清楚这一点而浪费了多少小时。

标签: android transparency toolbar android-coordinatorlayout android-collapsingtoolbarlayout


【解决方案1】:

正如hitch.united 指出的那样,它就像检查mColor 的值一样简单。浪费了这么多小时!希望这可以帮助其他人,如果他们曾经处于相同的位置!

【讨论】:

  • 这不回答这可能是对上述问题的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2016-04-28
  • 2014-01-11
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多