简体   繁体   English

Kotlin:如何使用Anko DSL制作工具栏菜单?

[英]Kotlin: How to make toolbar menu with Anko DSL?

How to make Android toolbar menu with Anko DSL directly in UI class? 如何在UI类中直接使用Anko DSL制作Android工具栏菜单? Do not want to write listeners in my Activity class. 不想在我的Activity类中编写侦听器。

Regarding my answer given below, is there a way to avoid XML recource file to describe menu items? 关于下面给出的答案,有没有一种方法可以避免使用XML源文件来描述菜单项?

Anko doesn't have any helpers for menu building yet. Anko还没有任何菜单构建帮助程序。 But there is an open Pull Request for that. 但是有一个开放的请求请求

Until that, you can use standard Kotlin/Android tools to create menu items in your code quite easily: 在此之前,您可以使用标准的Kotlin / Android工具轻松地在代码中创建菜单项:

class ActivityMainUi: AnkoComponent<ActivityMain> {
    override fun createView(ui: AnkoContext<ActivityMain>) = with(ui) {
        coordinatorLayout {
            appBarLayout {
                lparams(width = matchParent, height = wrapContent)

                toolbar {
                    lparams(width = matchParent, height = wrapContent)

                    menu.apply {
                        add("Action1").apply {
                            tooltipText = "Start Action 1"

                            // Unfortunately you cant't use `icon = R.drawable.ic_action_foo` here,
                            // because it would expect a Drawable instead of a Resource ID
                            setIcon(R.drawable.ic_action_foo)

                            setOnMenuItemClickListener {
                                startActivity<Activity1>()
                                true
                            }
                        }

                        add("Action2")
                                // If you don't like the extra apply,
                                // you can also use chain most of the setters
                                .setTooltipText("Start Action 2")
                                .setIcon(R.drawable.ic_action_bar)
                                .setOnMenuItemClickListener {
                                    startActivity<Activity2>()
                                    true
                                }
                                // Not all types of menu Icons do actually show the icon,
                                // so make it an Action for demo purposes
                                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) // <-- this is actually one of the setters, that cant be chained
                    }
                }
            }
        }
    }
}

In AndroidManifest.xml file select the theme without ActionBar AndroidManifest.xml文件中选择没有ActionBar的主题

Create your menu XML resource file as usual: 照常创建菜单XML资源文件:

<menu ...>
  <item
    android:id="@+id/action1"
    android:title="Action1"/>
  <item
    android:id="@+id/action2"
    android:title="Action2"/>
</menu>

For each item in menu create your custom activites 为菜单中的每个项目创建自定义活动

Create the main activity class: 创建主要活动类:

class ActivityMain : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       ActivityMainUI().setContentView(this)

       //do not use setActionBar() or setSupportActionBar()
    }

    // do not override onOptionsItemSelected() and onCreateOptionsMenu() here

 }


class ActivityMainUi: AnkoComponent<ActivityMain> {

    override fun createView(ui: AnkoContext<ActivityMain>) = with(ui) {

        coordinatorLayout {

            // justify layout

            appBarLayout {

                toolbar {

                    // justify your toolbar

                    inflateMenu(R.menu.your_menu_xml_resource)

                    onMenuItemClick { item ->
                        when (item!!.itemId) {
                            R.id.action_1 -> {
                                startActivity<Activity1>()
                                true
                            }
                            R.id.action_2 -> {
                                startActivity<Activity2>()
                                true
                            }
                            else -> false
                        }
                    }

                }.lparams(width = matchParent, height = wrapContent)

            }.lparams(width = matchParent, height = wrapContent)

            // put other views
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM