SearchBar中log_out_button图标仅短暂显示问题求助
问题:SearchBar点击后菜单图标短暂显示后消失
开发Android应用时,SearchBar关联了menu布局,其中的log_out_button图标应在点击SearchBar时显示,但实际仅短暂出现后就消失。实时编辑视图显示正常,但应用运行时问题明显。
相关代码片段
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>
menu.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 }
解决方案
问题根源是两处冲突操作+错误的组件使用方式:
- XML和代码重复加载菜单
- 将SearchBar当作ActionBar使用,干扰了它的原生逻辑
修改步骤
移除XML中的重复菜单配置
删除SearchBar标签里的app:menu="@menu/menu",避免重复加载导致的冲突。移除设置SupportActionBar的代码
删除(requireActivity() as AppCompatActivity).setSupportActionBar(binding.searchBar),SearchBar是独立组件,不需要依附ActionBar工作。绑定SearchView与SearchBar
给SearchView添加app:searchBar="@id/search_bar"属性,确保两者关联正常:<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" app:searchBar="@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>简化Fragment代码
保留菜单加载和点击逻辑即可,可额外添加SearchView关联代码确保功能完整:override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { binding = FragmentNoteListingBinding.inflate(layoutInflater) 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 } } // 可选:确保SearchView与SearchBar联动正常 binding.searchView.setupWithSearchBar(binding.searchBar) return binding.root }
修改完成后,点击SearchBar时菜单图标会稳定显示,同时SearchView也能正常展开工作。
内容的提问来源于stack exchange,提问作者Kostah
相关产品推荐
相关产品推荐

