问题描述
为什么我的操作栏没有显示在片段中?
我在菜单文件夹中添加了search.xml,这是search.xml代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_black_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="Search"/>
<item
android:id="@+id/chat"
android:icon="@drawable/ic_message_black_24dp"
app:showAsAction="ifRoom"
android:title="Message"/>
</menu>
public class HomeFragment extends Fragment implements RecyclerView.OnScrollchangelistener {
@Override
public void onCreateOptionsMenu(@NonNull Menu menu,@NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search,menu);
super.onCreateOptionsMenu(menu,inflater);
}
fragment_home.xml
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
tools:context=".ui.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
app:titleTextColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enteralways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="IFUNPOT" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_below="@+id/appbar"
android:id="@+id/recy_Feed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
解决方法
尝试将此行放入父级布局
android:layout_marginTop="?actionBarSize"
像这样
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?actionBarSize"
android:fitsSystemWindows="true"
tools:context=".ui.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
app:titleTextColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="IFUNPOT" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_below="@+id/appbar"
android:id="@+id/recy_feed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
,
在片段中添加:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
//....
}
来自doc:
setHasOptionsMenu
方法报告该片段希望通过接收对onCreateOptionsMenu(Menu,MenuInflater)
和相关方法的调用来参与填充选项菜单。