您好
我在其布局中有一个ViewPager活动。 ViewPager有5个碎片。 Fragments有一个带有图像的CollapsingToolbar和一个自定义ListView(强制使用NestedScrollChild)。现在我想在工具栏上显示一个选项菜单,但我无法让它工作。菜单没有显示。
在搜索解决方案时(也在这里)我发现,可以在三个代码位置中的一个中夸大菜单:
1:在片段的onCreate(Bundle savedInstanceState)
2:在片段onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
的视图中
3:在Fragmnent的onActivityCreated(Bundle savedInstanceState)
我已经尝试了所有这些,但结果总是一样 - >没有显示选项菜单。我在Log
中放置了the onCreateOptionsMenu(Menu menu, MenuInflater inflater)
但是logcat从未显示消息,因此从未到达过该函数。
有人可以帮我找到正确答案如何显示选项菜单吗?
来源:
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="de.example.test.MainActivity">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/part_navigation_toplevel"
android:id="@+id/pMain_INC_Toplevel" />
<android.support.v4.view.ViewPager
android:id="@+id/pMain_VP_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/pMain_INC_Toplevel"
android:background="@color/colorBackground" />
</RelativeLayout>
fragment_list.xml:
<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"
tools:context="de.example.test.MainActivity"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="@+id/pFragmentList_CC"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/pFragmentList_ABL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:background="@drawable/draw_bg_standard_element"
android:fitsSystemWindows="true"
android:layout_gravity="center_vertical"
app:contentScrim="@color/colorPrimary"
android:id="@+id/pFragmentList_CTB">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:background="@drawable/img_theme_blank"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<de.example.test.pakMainFragments.NestedScrollingListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="de.example.test.MainActivity"
android:id="@+id/pFragmentList_LV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="5.0sp"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:scrollbars="vertical"
android:paddingTop="@dimen/padding_normal" />
</android.support.design.widget.CoordinatorLayout>
menu_main_feed.xml:
<menu 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"
tools:context=".MainActivity">
<item
android:id="@+id/action_reload"
android:orderInCategory="100"
android:icon="@mipmap/ic_reload"
android:title="Reload"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="@+id/action_length"
android:orderInCategory="200"
android:title="timeperiode"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="@+id/action_showrelationships"
android:orderInCategory="300"
android:title="show relationships"
android:checkable="true"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="@+id/action_showcomments"
android:orderInCategory="400"
android:title="show comments"
android:checkable="true"
app:showAsAction="ifRoom|collapseActionView"
/>
</menu>
ListFragment.java(部分):
package de.example.test.pakMainFragments;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import de.example.test.R;
public class ListFragment extends Fragment {
// tested posibility 1:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_list,container, false);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) v.findViewById(R.id.pFragmentList_CTB);
collapsingToolbarLayout.setTitle(getString(R.string.sTitleFeed));
NestedScrollingListView lvList = (NestedScrollingListView) v.findViewById(R.id.pFragmentList_LV);
/* removed for better reading */
// tested posibility 2: in onCreate()
setHasOptionsMenu(true);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main_feed, menu);
Log.e("ListFragment", "onCreateOptionsMenu");
super.onCreateOptionsMenu(menu, inflater);
}
// tested posibility 3:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
public static ListFragment newInstance(String sText) {
FeedFragment fList = new ListFragment();
Bundle bList = new Bundle();
bList.putString("msg", sText);
fList.setArguments(bList);
return fList;
}
}
谢谢!
修改
我使用没有ActionBar的样式。现在我尝试使用actionBar的样式,我可以看到MainActivity的ActionBar中的选项菜单,但是我想要它在片段的折叠工具栏中...我怎么能解决这个问题?
style.xml:
<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>
</resources>
Fragment中的工具栏样式在Fragment layout xml
中被覆盖