第一行代码 按书配置Menu不出来

2024-05-24 17:28

本文主要是介绍第一行代码 按书配置Menu不出来,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题:按照书本配置Menu,就是不出来

页面activity 源码

重写了:onCreateOptionsMenu(), onOptionsItemSelected()

package com.example.lanidemoktimport android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.catchpig.utils.LogUtils
import com.example.lanidemokt.adapter.OrderListAdapter
import com.example.lanidemokt.databinding.ActivityMenuTextBinding
import com.example.lanidemokt.databinding.ActivityOrderListBinding
import com.example.lanidemokt.viewmodel.ButtonClickListener
import com.example.lanidemokt.viewmodel.MenuTestViewModel
import com.example.lanidemokt.viewmodel.OrderItemViewModel
import com.example.lanidemokt.viewmodel.OrderListViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.math.logclass MenuTestActivity : AppCompatActivity() {var binding: ActivityMenuTextBinding? = null // 操作布局实例var vm = MenuTestViewModel()override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState) //        setContentView(R.layout.activity_main)binding = DataBindingUtil.setContentView(this, R.layout.activity_menu_text)binding?.vm = vminitView()initData()test()}private fun test() { //coroutineScope}private fun initData() {}private fun initView() {}override fun onCreateOptionsMenu(menu: Menu?): Boolean { //        return super.onCreateOptionsMenu(menu)menuInflater.inflate(R.menu.main_menu_item_list, menu)return true}override fun onContextItemSelected(item: MenuItem): Boolean {LogUtils.d("点击了菜单")when (item.itemId) {R.id.more_item -> Toast.makeText(this, "更多", Toast.LENGTH_LONG).show()R.id.more_edit -> Toast.makeText(this, "编辑", Toast.LENGTH_LONG).show()}return true}}

布局activity_menu_text.xml 源码

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><data><import type="android.view.View" /><variablename="order"type="com.example.lanidemokt.viewmodel.OrderItemViewModel" /><variablename="vm"type="com.example.lanidemokt.viewmodel.MenuTestViewModel" /></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/msg2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="菜单页面"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:text="消息" /><Buttonandroid:id="@+id/login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="订单列表-点击事件-adapter实现"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/msg2" /><TextViewandroid:id="@+id/msg5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="@{vm.msg}"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/login"tools:text="消息2" /><!--      <androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@id/msg5"MenuTestAdapter_bindList="@{vm.orderList}"tools:listitem="@layout/b_order_item" /> --><androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@id/msg5"tools:listitem="@layout/b_order_item" /></androidx.constraintlayout.widget.ConstraintLayout>
</layout>

菜单项目列表xml 源码

res/menu/main_menu_item_list.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"><item 
android:id="@+id/add_item" 
android:title="Add"/> <item 
android:id="@+id/remove_item" 
android:title="Remove"/> 
</menu>

解决:

看效果图,根本没有菜单栏出来,更不会出现右侧的菜单栏了。

看一下主题配置,原来是之前配置主题颜色时,修改了主题,使用了自己自定义的主题文件:(res/values/themes.xml),

<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="Base.Theme.LaniDemoKt" parent="Theme.Material3.DayNight.NoActionBar"><!-- Customize your light theme here. 自定义主题,颜色等--><!-- <item name="colorPrimary">@color/my_light_primary</item> --></style><style name="Theme.LaniDemoKt" parent="Base.Theme.LaniDemoKt" />
</resources>

主题,需要使用AppCompat的主题,将:android:theme="@style/Theme.LaniDemoKt", 修改为:

android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar
   <applicationandroid:name=".DemoApplication"android:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:networkSecurityConfig="@xml/network_security_config"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.LaniDemoKt"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"android:usesCleartextTraffic="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".OrderListActivity" /><activity android:name=".MenuTestActivity" /></application>

 再次运行。

最终效果

这篇关于第一行代码 按书配置Menu不出来的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999051

相关文章

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

mysql5.7.15winx64配置全过程

《mysql5.7.15winx64配置全过程》文章详细介绍了MySQL5.7.15免安装版的配置步骤,包括解压安装包、设置环境变量、修改配置文件、初始化数据目录、安装服务、启动数据库、登录及密码修改... 目录前言一、首先下载安装包二、安android装步骤1.第一步解压文件2.配置环境变量3.复制my-

MySQL实现多源复制的示例代码

《MySQL实现多源复制的示例代码》MySQL的多源复制允许一个从服务器从多个主服务器复制数据,这在需要将多个数据源汇聚到一个数据库实例时非常有用,下面就来详细的介绍一下,感兴趣的可以了解一下... 目录一、多源复制原理二、多源复制配置步骤2.1 主服务器配置Master1配置Master2配置2.2 从服

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据