Toolbar Kotlin 简单使用

本文介绍了在Android中使用Kotlin进行Toolbar的简单应用,包括添加依赖、创建和配置Toolbar,以及添加菜单项。在实践过程中遇到菜单栏显示问题,通过注释setSupportActionBar(mToolbar)解决了,但引发标题居中异常,此现象有待进一步探究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

1.在build.gradle添加依赖

ext.anko_version = '0.10.1'
apply plugin: 'kotlin-android-extensions'
compile "org.jetbrains.anko:anko-commons:$anko_version"

2.将AppTheme改为继承noActionBar

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

创建toolbar

编辑 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.zyqzyq.myapplication.MainActivity">
    <android.support.v7.widget.Toolbar
            android:id="@+id/mToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:contentInsetStart="0dp">
        <TextView
                android:id="@+id/mTitle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="标题"
                android:textSize="36sp"
                android:gravity="center"/>

    </android.support.v7.widget.Toolbar>
    <TextView
            android:id="@+id/mTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:layout_centerInParent="true"/>

</RelativeLayout>

修改MainAcitivity.kt
在MainActivity.kt 中添加如下代码

//将mtoolbar设置为actionbar
setSupportActionBar(mToolbar)
//APP 图标
mToolbar.setLogo(R.drawable.ic_launcher)
//APP 标题
mToolbar.title = ""
mTitle.text = "标题居中"
//APP 正文内容
mTextView.text = "正文内容"

添加menu

新建 /res/menu/menu_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/app_bar_search"
            android:icon="@drawable/ic_search_black_24dp"
            android:title="Search" 
            android:actionViewClass="android.widget.SearchView" 
            app:showAsAction="ifRoom"/>
    <item android:title="Item" app:showAsAction="never"/>
    <item android:title="Item" app:showAsAction="never"/>
</menu>

将menu加入toolbar并在MainActivity.kt中添加监听。

//APP 菜单栏
mToolbar.inflateMenu(R.menu.menu_toolbar)
//菜单栏监听
mToolbar.setOnMenuItemClickListener {
    when(it.itemId){
        R.id.app_bar_search -> toast("search")
        else -> toast("click something else")
    }
    true
}

菜单栏未正常显示,注释setSupportActionBar(mToolbar)后可以正常显示(暂未找到原因,有懂的希望告知下,谢谢)

运行代码后成功界面
这里写图片描述

(莫名的发现标题不居中了,注销了setSupportActionBar以后居中正常显示,感觉有毒,还需在研究研究)

个人博客地址:https://zyqzyq.github.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值