如何解决为什么我的操作栏不显示在片段中?
为什么我的操作栏没有显示在片段中?
我在菜单文件夹中添加了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)
和相关方法的调用来参与填充选项菜单。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。