Fragment 的工具栏在 Activity 的工具栏上透明
Fragment's Toolbar transparent over Activity's toolbar
我有一个我似乎无法弄清楚的特殊情况,我有预感它涉及 CollapsingToolbarLayout
。
我有一个 activity 具有以下 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);
}
为什么会出现这个问题?
正如 hitch.united 指出的那样,它就像检查 mColor
的值一样简单。浪费了那么多小时!希望这可以帮助其他人,如果他们曾经处于相同的位置!
我有一个我似乎无法弄清楚的特殊情况,我有预感它涉及 CollapsingToolbarLayout
。
我有一个 activity 具有以下 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);
}
为什么会出现这个问题?
正如 hitch.united 指出的那样,它就像检查 mColor
的值一样简单。浪费了那么多小时!希望这可以帮助其他人,如果他们曾经处于相同的位置!