问题:按照书本配置Menu,就是不出来
页面activity 源码
重写了:onCreateOptionsMenu(), onOptionsItemSelected()
package com.example.lanidemokt
import 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.log
class 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 = vm
initView()
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" />
<variable
name="order"
type="com.example.lanidemokt.viewmodel.OrderItemViewModel" />
<variable
name="vm"
type="com.example.lanidemokt.viewmodel.MenuTestViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android: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="消息" />
<Button
android: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" />
<TextView
android: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.RecyclerView
android: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.RecyclerView
android: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
<application
android: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">
<activity
android: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>
再次运行。
文章评论