英文:
Add android toolbar in fragment with No Action bar (Icons are not showing)
问题
我想在工具栏的左侧添加一个通知图标,在右侧添加一个搜索图标。
我尝试了以下方法,但不起作用。
我想实现类似于这样的效果:
我想要做类似于这样的效果。
我的代码:
fragment_browse.xml
<androidx.appcompat.widget.Toolbar
    android:id="@+id/tb_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="?attr/colorAccent"
    android:minHeight="?attr/actionBarSize"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:title="Browse" />
在 BrowseFragment.kt 中:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.browse_menu, menu)
    val searchItem = menu.findItem(R.id.appSearchBar)
    val searchManager = activity?.getSystemService(Context.SEARCH_SERVICE) as SearchManager
    if (searchItem != null) {
        searchView = searchItem.actionView as SearchView
    }
    if (searchView != null) {
        searchView!!.setSearchableInfo(searchManager.getSearchableInfo(activity!!.componentName))
        queryTextListener = object : SearchView.OnQueryTextListener {
            override fun onQueryTextChange(newText: String): Boolean {
                Log.i("onQueryTextChange", newText)
                return true
            }
            override fun onQueryTextSubmit(query: String): Boolean {
                Log.i("onQueryTextSubmit", query)
                return true
            }
        }
        searchView!!.setOnQueryTextListener(queryTextListener)
    }
    super.onCreateOptionsMenu(menu, inflater)
}
我的 onCreateView 方法:
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_browse, container, false)
    var toolbar = view.findViewById(R.id.tb_toolbar) as Toolbar
    (activity as AppCompatActivity?)!!.setSupportActionBar(toolbar)
    
    return view
}
英文:
I want to add a notification icon on the left and search icon on the right of the toolbar.
I approach the following way but does not work
I want to make something like this
My Code:
fragment_browse.xml
 <androidx.appcompat.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="?attr/colorAccent"
            android:minHeight="?attr/actionBarSize"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:theme="@style/ThemeOverlay.AppCompat.Dark"
            app:title="Browse" />
Inside BrowseFragment.kt
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.browse_menu, menu)
    val searchItem = menu.findItem(R.id.appSearchBar)
    val searchManager = activity?.getSystemService(Context.SEARCH_SERVICE) as SearchManager
    if (searchItem != null) {
        searchView = searchItem.actionView as SearchView
    }
    if (searchView != null) {
        searchView!!.setSearchableInfo(searchManager.getSearchableInfo(activity!!.componentName))
        queryTextListener = object : SearchView.OnQueryTextListener {
            override fun onQueryTextChange(newText: String): Boolean {
                Log.i("onQueryTextChange", newText)
                return true
            }
            override fun onQueryTextSubmit(query: String): Boolean {
                Log.i("onQueryTextSubmit", query)
                return true
            }
        }
        searchView!!.setOnQueryTextListener(queryTextListener)
    }
    super.onCreateOptionsMenu(menu, inflater)
}
My OnCreateView method
 override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_browse, container, false)
    var toolbar = view.findViewById(R.id.tb_toolbar) as Toolbar
    (activity as AppCompatActivity?)!!.setSupportActionBar(toolbar)
    
return view
}
答案1
得分: 0
在我看来,你可以简单地为现有工具栏膨胀菜单(而不是使用 setSupportActionBar)。
以下是示例:
        toolbar.inflateMenu(R.menu.browse_menu)
        // 这将以编程方式膨胀你的菜单
        val searchItem = toolbar.menu.findItem(R.id.appSearchBar)
        // 在这里使用菜单项进行你的代码处理
        toolBar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
        // 或者在这里放入你的图标ID - 这是左侧图标
        toolBar.setNavigationOnClickListener { view ->
        // 这是左侧图标的点击处理程序
        }
这样一来,你就不需要重写 onCreateOptionsMenu,也不需要通过活动进行操作。
英文:
In my opinion you could simply inflate the menu for existing toolbar (instead of setSupportActionBar).
Here is the example:
        toolbar.inflateMenu(R.menu.browse_menu)
        //this inflates your menu programmatically
        val searchItem = toolbar.menu.findItem(R.id.appSearchBar)
        //do your code with menuitem here
        toolBar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
        //or put there your icon ID - that's for the icon at the left side
        toolBar.setNavigationOnClickListener { view ->
        //that's the click handler for the icon at the left side
        }
And you need no override for onCreateOptionsMenu and no activity actions this way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论