【问题标题】:How to show menu item as action on a toolbar in a fragment如何将菜单项显示为片段中工具栏上的操作
【发布时间】:2020-03-24 02:41:51
【问题描述】:

我已经设置了一个基本菜单和一个工具栏,如下所示:

home_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/notifications"
        app:showAsAction="always"
        android:title="@string/_ui_notifications"
        android:icon="@drawable/ic_notifications_active"
        />


</menu>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="._fragments.dash.HomeFragment"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="64dp">
        <Toolbar
            android:id="@+id/homeToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/app_logo_new"
                android:scaleType="fitCenter"
                android:padding="16dp"/>
        </Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    <ScrollView
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="240dp"/>
        </LinearLayout>



    </ScrollView>

</LinearLayout>

HomeFragment.kt


class HomeFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_home, container, false)
        appToolbar = view.findViewById(R.id.homeToolbar)
        appToolbar.title = ""
        appToolbar.inflateMenu(R.menu.home_menu)
        appToolbar.setOnMenuItemClickListener{
            when(it.itemId){
                R.id.notifications -> {
                    Toast.makeText(activity!!, "Notifications", Toast.LENGTH_SHORT).show()
                    true
                }
                else -> {
                    Log.d("Menu", "Terrible things have happened in menu")
                    false
                }
            }

        }

        return view
    }

}

通过这个简单的设置,我能够让工具栏正常工作。同样点击菜单项“通知”会按预期发出祝酒词。

但是,菜单显示为下拉菜单,如下所示:

如何将其转化为行动?

编辑:

  • 我已经使用属性.NoActionBar禁用了主题中的默认操作栏

  • -

【问题讨论】:

  • 您希望如何显示在下拉菜单或操作栏上?
  • 我想要一个通知按钮的图标。不下拉
  • 您可以通过在菜单项中将 showAsAction 值设置为“ifRoom”来进行更改

标签: android user-interface kotlin menu


【解决方案1】:

您在布局中使用原生 Toolbar;即&lt;Toolbar&gt;。这将忽略菜单 XML 中的 app:showAsAction 属性,因为它位于应用的命名空间中。

我猜你实际上打算使用库Toolbar,在这种情况下,你想将该布局元素更改为&lt;androidx.appcompat.widget.Toolbar&gt;,并在HomeFragment 类中修改相关的import 语句.

如果你真的想使用原生的Toolbar,出于某种原因,你需要使用android命名空间中的showAsAction属性;即android:showAsAction

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2017-01-23
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多