Android Studio 为什么菜单不出现在Android应用程序?

0x6upsns  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(122)

我实现了我创建的菜单的代码。它不会出现在模拟器中,出现的只是回收器视图。有人知道是什么导致了这个问题吗?
我在模拟器上使用API 29 Android版本5。我选择的Android版本重要吗?
下面是我代码:

menu_main.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/item1"
        android:icon="@drawable/ic_icon"
        android:title="About Me"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/item2"
        android:title="List"
        app:showAsAction="never" />

    <item
        android:id="@+id/item3"
        android:title="Grid"
        app:showAsAction="never">

    </item>

</menu>

菜单活动.kt

package com.example.bangkit_recycleview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private lateinit var foodRecycleView : RecyclerView
private lateinit var foodArrayList : ArrayList<FoodDetail>
lateinit var imageArticle : Array<Int>
lateinit var heading : Array<String>
lateinit var description : Array<String>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    imageArticle = arrayOf(
        R.drawable.apfelstrudel,
        R.drawable.kebab,
        R.drawable.kimchi,
        R.drawable.lasagna,
        R.drawable.rendang,
        R.drawable.sushi,
        R.drawable.tom_yam,
        R.drawable.paella,
        R.drawable.pecking_duck,
        R.drawable.pho,
    )

    heading = arrayOf(
        "Apfelstrudel",
        "Kebab Turkey",
        "Kimchi",
        "Lasagna",
        "Rendang",
        "Sushi",
        "Tom Yam",
        "Paella",
        "Pecking Duck",
        "Pho",
    )

    description = arrayOf(
        "More commonly known as Apple Strudel, Apfelstrudel is a popular pastry in Austria and other parts of Europe. The dish consists of an oval strudel pastry cover with mouthwatering apple filling inside. The apple filling is prepared with grated apples, cinnamon, sugar, breadcrumbs, and raisins. This delicacy tastes best when served with vanilla ice cream and whipped cream. ",
        "A dish popular across the Middle East, Kebabs are originally from Turkey. They consist of ground meat or seafood, fruits, and vegetables in some cases and are cooked on a skewer with a big fire underneath, just like a barbeque on the grill",
        "Kimchi is a staple Korean side dish prepared from fermented vegetables such as Korean radishes, and cabbage and topped with several seasonings, including garlic, chilli powder, scallions, and ginger.",
        "Italy's lasagna takes over Pizza to be added in the \"world's best food dishes\" list because of its comeback. It is one of the oldest pasta but has become popular only in the present times. The ingredients itself sound mouthwatering - meats, pasta, vegetables, tomato sauce, and lots and lots of cheese.",
        "Often called \"the world's most delicious dish,\" Rendang is prepared by simmering beef with coconut milk with a mixture of the best of spices including turmeric, garlic, lemongrass, ginger, chillies, and galangal. The dish is then stewed for a few hours which gives it a tender texture and exotic taste. The blast of flavours is surely one of the reasons why the dish is loved globally and is also one of the best dishes in the world. Easy to rustle up, this dish is often served at ceremonies or to honour guests.",
        "Prepared with vinegared rice and a wide range of ingredients including seafood, vegetables, and sometimes fruits. Sushi tastes best when served with wasabi, pickled ginger, and soy sauce. A popular garnish for this dish is Daikon radish. The type of fish in it defines a sushi's taste. However, the vinegared rice gives the dish a tangy taste overall. ",
        "A type of sour and hot Thai soup, Tom yam goong is prepared with shrimp along with a load of healthy herbs and spices?  lemongrass, lime, kaffir leaves, galangal, and red chili peppers to name a few. the soup plays around with a bit of all possible flavours? spicy, sour, salty and sweet. ",
        "Paella has got its roots in Valencia, Spain. It is an ancient dish recreated with a modern touch in the present times. There are various ways to eat Paella. The original recipe contains white rice with green beans, meat (rabbit or chicken, sometimes duck), butterbeans, snails, topped with seasoning such as rosemary and. The recipe also uses artichokes during its season. ",
        "Peking duck is a dish that finds its way to Beijing. The ducks for this dish are specially bred and slaughtered after 60 days and seasoned first before being roasted in closed ovens. This gives the meat a crisp skin and thin texture. The dish is served with cucumbers, spring onion, and sweet bean sauce. ",
        "A simple yet an incredible dish, Pho (pronounced as 'fuh') is a Vietnamese dish made of rice noodles and meat (usually beef or chicken) served in broth and topped with herbs. The dish has got a great fragrance which lingers in the eater's head for a while. ",
    )

    foodRecycleView = findViewById(R.id.recycleView)
    foodRecycleView.layoutManager = LinearLayoutManager(this)
    foodRecycleView.setHasFixedSize(true)

    foodArrayList = arrayListOf<FoodDetail>()
    getFoodData()
}

private fun getFoodData() {
    for(i in imageArticle.indices) {
        val foods = FoodDetail(imageArticle[i], heading[i], description[i])
        foodArrayList.add(foods)
    }

    var adapter = FoodAdapter(foodArrayList)
    foodRecycleView.adapter = adapter
    adapter.setOnItemClickCallback(object : FoodAdapter.OnItemClickCallback {
        override fun onItemClicked(data: FoodDetail) {
            showSelectedHero(data)
        }

    })
}

private fun showSelectedHero(food: FoodDetail) {
    Toast.makeText(this, "Kamu memilih " + food.heading, Toast.LENGTH_SHORT).show()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu_main, menu)
    return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.item1 -> {
            Toast.makeText(this, "Item 1 selected", Toast.LENGTH_SHORT).show()
            return true
        }
        R.id.item2 -> {
            Toast.makeText(this, "Item 2 selected", Toast.LENGTH_SHORT).show()
            return true
        }
        R.id.item3 -> {
            Toast.makeText(this, "Item 3 selected", Toast.LENGTH_SHORT).show()
            return true
        }
        else -> return super.onOptionsItemSelected(item)
    }
} }

谁能帮我解决这个问题?

gev0vcfq

gev0vcfq1#

我们真的需要看到AndroidManifest.xmlthemes.xmlactivity_main.xml才能确切地知道发生了什么。
最有可能的问题是,您正在为MainActivity使用.NoActionBar主题,但没有在activity_main.xml中提供显式工具栏(如MaterialToolBar)。在这种情况下,您根本看不到工具栏(顶部没有标题)。

相关问题