在片段中添加 Android 工具栏,但不显示操作栏(图标未显示)。

huangapple go评论72阅读模式
英文:

Add android toolbar in fragment with No Action bar (Icons are not showing)

问题

我想在工具栏的左侧添加一个通知图标,在右侧添加一个搜索图标。

我尝试了以下方法,但不起作用。

我想实现类似于这样的效果:

在片段中添加 Android 工具栏,但不显示操作栏(图标未显示)。

我想要做类似于这样的效果。

我的代码:

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

在片段中添加 Android 工具栏,但不显示操作栏(图标未显示)。

I want to make something like this

My Code:

fragment_browse.xml

 &lt;androidx.appcompat.widget.Toolbar
            android:id=&quot;@+id/tb_toolbar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:gravity=&quot;center&quot;
            android:background=&quot;?attr/colorAccent&quot;
            android:minHeight=&quot;?attr/actionBarSize&quot;
            app:actionViewClass=&quot;android.support.v7.widget.SearchView&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            app:theme=&quot;@style/ThemeOverlay.AppCompat.Dark&quot;
            app:title=&quot;Browse&quot; /&gt;

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(&quot;onQueryTextChange&quot;, newText)
                return true
            }

            override fun onQueryTextSubmit(query: String): Boolean {
                Log.i(&quot;onQueryTextSubmit&quot;, 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&#39;s for the icon at the left side
        toolBar.setNavigationOnClickListener { view -&gt;
        //that&#39;s the click handler for the icon at the left side
        }

And you need no override for onCreateOptionsMenu and no activity actions this way.

huangapple
  • 本文由 发表于 2020年9月25日 03:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64053464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定