菜单不会显示在搜索栏中

问题描述 投票:0回答:1

我目前在应用程序中遇到搜索栏问题。搜索栏是菜单中膨胀布局的一部分。在此菜单中,有一个名为“log_out_button”的图标,单击该图标后应该会出现在搜索栏中。但是,此图标仅在单击搜索栏时短暂显示。

我检查了实时编辑视图,一切似乎都很好:

但是,当我实际启动应用程序时,问题就变得明显了:这里有一个视频演示。

以下是相关代码片段:

fragment_note_listing.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.search.SearchBar
                android:id="@+id/search_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/search_your_notes"
                app:backgroundTint="?attr/colorBackgroundFloating"
                app:menu="@menu/menu"
                app:navigationIcon="@drawable/ic_menu" />
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view_of_notes"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginVertical="16dp"
            android:orientation="vertical"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/app_bar" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floating_action_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginBottom="24dp"
            android:clickable="true"
            android:contentDescription="@string/add_note_button"
            android:focusable="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@drawable/ic_add" />    

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.search.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/search_your_notes"
        android:inputType="text"
        android:maxLines="1"
        app:backgroundTint="?attr/colorBackgroundFloating"
        app:layout_anchor="@id/search_bar">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:backgroundTint="?android:attr/colorBackground" />

    </com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

菜单.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/log_out_button"
        android:contentDescription="@string/log_out_button"
        android:title="@string/log_out_button"
        android:icon="@drawable/ic_log_out"
        app:showAsAction="always" />
</menu>

NoteListingFragment 中的 onCreateView 函数

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FragmentNoteListingBinding.inflate(layoutInflater)
        (requireActivity() as AppCompatActivity).setSupportActionBar(binding.searchBar)
        binding.searchBar.inflateMenu(R.menu.menu)
        binding.searchBar.setOnMenuItemClickListener { menuItem ->
            when (menuItem.itemId) {
                R.id.log_out_button -> {
                    authenticationViewModel.logout()
                    findNavController().navigate(R.id.action_noteListingFragment_to_welcomeFragment)
                    true
                }
                else -> false
            }
        }
        return binding.root
    }

有菜单布局

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/log_out_button"
        android:contentDescription="@string/log_out_button"
        android:title="@string/log_out_button"
        android:icon="@drawable/ic_log_out"
        app:showAsAction="always" />
</menu>
android kotlin android-layout android-menu android-search
1个回答
0
投票

修改工具栏而不是使用搜索栏,

setSupportActionBar(binding.searchBar);
    


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
}

就活动而言,膨胀正常菜单,

它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.