简体   繁体   English

空活动中缺少操作栏

[英]missing action bar in empty activity

I want to learn how to work with toolbar in android and trying to add menu in action bar but when I ran my previews started project, noticed that default empty activity menu bar now missing! 我想学习如何在android中使用工具栏并尝试在操作栏中添加菜单,但是当我运行预览启动的项目时,注意到默认的空活动菜单栏现在丢失了! I don't know why this happened. 我不知道为什么会这样。

here is my MainActivity.xml code 这是我的MainActivity.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ir.cupcode.artin.contact.MainActivity">
<ListView
    android:id="@+id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</RelativeLayout>

and I have this block of code for menu bar in MainActivity.java : 而且我在MainActivity.java中有菜单栏的这段代码:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menubar, menu);
    return true;
}

here is my menubar.xml code 这是我的menubar.xml代码

 <?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/action_refresh"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_test"
    android:title="Refresh"/>
<item
    android:id="@+id/action_settings"
    android:title="Settings">
</item>

In addition I get an error for using android:showAsAction="always" that says should use app:showAsAction with the appcombat library 另外,使用android:showAsAction="always"出现错误,提示应将app:showAsAction与appcombat库一起使用

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_appicon2"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>

Change your app theme 更改您的应用主题

  android:theme="@style/Theme.AppCompat.Light.NoActionBar"

into 进入

  android:theme="@style/AppTheme"

Change you theme from : 从更改主题:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

to : 至 :

android:theme="@style/Theme.AppCompat.Light.DarkActionBar"

First you have to add a toolbar in your MainActivity.xml, inside your Relative Layout and over your ListView. 首先,您必须在MainActivity.xml中的“相对布局”内部和ListView上方添加一个工具栏。 You can create a new file for that like naming it 'Toolbar.xml' and creating the content like this: 您可以为此创建一个新文件,例如将其命名为“ Toolbar.xml”并创建如下内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Main Activity"/>

then include it in your MainActivity.xml: 然后将其包含在您的MainActivity.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ir.cupcode.artin.contact.MainActivity">

<include layout="@layout/toolbar" id="@+id/toolbar">

<ListView
    android:id="@+id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</RelativeLayout>

Then you have to instantiate the toolbar in your app first. 然后,您必须首先在应用程序中实例化工具栏。 Like this: 像这样:

yourToolbar = (Toolbar) findViewById(R.id.yourToolbar);
setSupportActionbar(yourToolbar);

You can also check out these forums. 您也可以查看这些论坛。

Toolbar.inflateMenu seems to do nothing Toolbar.inflateMenu似乎无能为力

How to set menu to Toolbar in Android 如何在Android中将菜单设置为工具栏

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM