微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的操作栏不显示在片段中?

如何解决为什么我的操作栏不显示在片段中?

为什么我的操作栏没有显示在片段中?

我在菜单文件夹中添加了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 举报,一经查实,本站将立刻删除。