动作栏在Android应用中显示为空 [英] Action Bar shows up empty in Android app

查看:90
本文介绍了动作栏在Android应用中显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将带有菜单按钮的操作栏放入单个android活动中.我已将其放入活动XML:

I want to put an action bar with a menu button into a single android activity. I've put this in my activity XML:

<android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />

我已将其放在我的JAVA文件中:

I've put this in my JAVA file:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.chat_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.view_profile:
                finish();
                return true;
            case R.id.report_match:
                finish();
                return true;
            case R.id.add_match:
                finish();
                return true;
            case R.id.unmatch:
                finish();
                return true;


            default:
                return super.onOptionsItemSelected(item);
        }
    }

我已将其放入菜单XML

And I've put this in the menu XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/title"
        android:title="@string/options"
        android:showAsAction="never"
        tools:ignore="AppCompatResource" />
    <item android:id="@+id/view_profile"
        android:title="@string/view_profile" />
    <item android:id="@+id/report_match"
        android:title="@string/report_match" />
    <item android:id="@+id/add_match"
        android:title="@string/add_match" />
    <item android:id="@+id/unmatch"
        android:title="@string/unmatch" />
</menu>

当前,当我启动应用程序时,操作栏显示为空白(图像) 这很奇怪,因为当我编辑菜单XML时,预览会以我希望的方式显示它( IMAGE ).预先感谢!

Currently the action bar shows up empty when I launch the app (IMAGE) Which is strange because when I edit the menu XML the preview shows it the way I want it to (IMAGE). Thanks in advance!

推荐答案

您是否已将工具栏添加到活动中?

Have you added the Toolbar to the activity?

工具栏myToolbar =(工具栏)findViewById(R.id.my_toolbar);

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);

setSupportActionBar(myToolbar);

setSupportActionBar(myToolbar);

有关设置工具栏的更多信息

这篇关于动作栏在Android应用中显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆