简体   繁体   English

Android 操作栏不可见

[英]Android actionbar not visible

I am trying to add an option menu in actionbar to my existing app but it is not working.我正在尝试将操作栏中的选项菜单添加到我现有的应用程序中,但它不起作用。 If I create a new project with "hello world" default app I can see the button in action bar.如果我使用“hello world”默认应用程序创建一个新项目,我可以在操作栏中看到该按钮。 The onCreateOptionMenu() method seems never caught in debug with breakpoint, what's wrong ?? onCreateOptionMenu() 方法似乎从未在带断点的调试中陷入困境,怎么了?

I'm working with API 14 on both app and this is my MainActivity code.我正在两个应用程序上使用 API 14,这是我的 MainActivity 代码。

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class Principale extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ecran_d_acceuil);


}
public boolean onCreateOptionMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.principale, menu);
    return true;
}
 }

My menu.xml code我的 menu.xml 代码

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
</menu>

manifest.xml清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapp.Principale"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.myapp.Home"
            android:label="@string/title_activity_home" >
        </activity>
        <activity
            android:name="com.example.myapp.ListDir"
            android:label="@string/title_activity_list_dir" >
        </activity>
    </application>
</manifest>

In my case, what fixed the issue, was the answer of this question ;就我而言,解决问题的是这个问题的答案; to remove去除

android:theme="@style/AppTheme"

from the tag in the Manifest.从清单中的标签。

Have you tried to set the app theme to a theme that supports the ActionBar , like Theme.Holo.Light ?您是否尝试将应用程序主题设置为支持ActionBar的主题,例如Theme.Holo.Light

Also, in your Activity you can try getActionBar().show();此外,在您的Activity您可以尝试getActionBar().show(); . .

Principale 扩展 ActionBarActivity 应该处理的问题

You have extended Activity.您已扩展活动。 Try extending ActionBarActivity in java code尝试在 java 代码中扩展 ActionBarActivity

Spent 3 hours and eventually fixed it!花了3个小时终于修好了! Forget removing android:theme="@style/AppTheme" from your manifest.忘记从清单中删除 android:theme="@style/AppTheme" 。 It makes run-time error.它会导致运行时错误。 This is how I fixed the problem.这就是我解决问题的方法。 Your activity should extend AppCompatActivity instead of Activity.您的 Activity 应该扩展 AppCompatActivity 而不是 Activity。

For your information, I solved my problem of the actionBar by going on the layout XML file and then pressing on View Option -> Show Layout Decorations.供您参考,我通过打开布局 XML 文件,然后按“查看选项”->“显示布局装饰”解决了我的 actionBar 问题。

View Option 是 Select Design Surface 下方的小眼睛

Hope it will solve your problem.希望它能解决你的问题。

Cheers.干杯。

检查您的清单文件在活动声明中,如果此行存在,则将其删除。

android:theme="@style/AppTheme.NoActionBar"

your main activity needs to be like this:你的主要活动需要是这样的:

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getActionBar();
    actionBar.hide();

    actionBar.show();
    actionBar.setSubtitle("subtitle");
    actionBar.setTitle("title"); 


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_refresh:
      Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
          .show();
      break;

         default:
             break;
             }
    return true;
    } 



}

and your main.xml like this:和你的 main.xml 像这样:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

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

</menu> 

this is a good and simple guide to creating an action bar: http://www.vogella.com/tutorials/AndroidActionBar/article.html这是创建操作栏的简单指南: http : //www.vogella.com/tutorials/AndroidActionBar/article.html

on create of your activity.在创建您的活动时。

getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        customNav = LayoutInflater.from(this).inflate(
                R.layout.action_bar_customvieew, null);
        getSupportActionBar().setCustomView(customNav);

What solved my issue in this regard was, in the design view , click the eye icon ( view options ) and select Show Layout Decorations .在这方面解决我的问题的是,在design view ,单击eye图标( view options )并选择Show Layout Decorations

显示 Android Studio 3.5 IDE 中的眼睛图标的图像

Make sure you don't use:确保您不使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);

in your layout .xml file.在您的布局 .xml 文件中。

在我的情况下,操作栏菜单在使用 public class MainActivity extends Activity { } 时不可见,但在使用 public class MainActivity extends ActionBarActivity{ } 时可见

我遇到了同样的问题,我确认如果您从AndroidManifest.xml删除android:theme="@style/AppTheme" ,它将解决您的问题。

您应该扩展AppCompatActivity

you have to call the method setHasOptionsMenu(true);你必须调用方法 setHasOptionsMenu(true); in the OnCreate Method在 OnCreate 方法中

我正在使用三星年轻 2 作为 android 模拟器,我有同样的问题,我通过从 manifest.xml 中删除android:theme =""并将主题更改为另一个主题支持操作栏来解决这个问题

getSupportActionBar().show();

只需将此代码粘贴到您的 java onCreate函数中即可。

My Android studio version is 4.2.1 for Windows 64-bit (933 MiB).我的 Android Studio 版本是 4.2.1,适用于 Windows 64 位 (933 MiB)。 In my case, I did not have the "Show Layout Decorations" option in the design view.就我而言,我在设计视图中没有“显示布局装饰”选项。 What I did is I solved my problem of the action bar by going on the layout XML file and then pressing on View Option -> Show System UI我所做的是通过打开布局 XML 文件然后按下查看选项 -> 显示系统 UI 来解决我的操作栏问题

ActionBar visibility in android studio: android studio中ActionBar的可见性:

在此处输入图片说明

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

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