Android actionbar not visible

后端 未结 17 2226
萌比男神i
萌比男神i 2020-12-18 18:23

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 a

相关标签:
17条回答
  • 2020-12-18 18:44

    Have you tried to set the app theme to a theme that supports the ActionBar, like Theme.Holo.Light?

    Also, in your Activity you can try getActionBar().show();.

    0 讨论(0)
  • 2020-12-18 18:44

    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.

    0 讨论(0)
  • 2020-12-18 18:44

    Check Your manifest file In the Activity declaration if this line is there then delete it.

    android:theme="@style/AppTheme.NoActionBar"
    
    0 讨论(0)
  • 2020-12-18 18:47

    Make sure you don't use:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    in your layout .xml file.

    0 讨论(0)
  • 2020-12-18 18:50

    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:

     <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

    0 讨论(0)
  • 2020-12-18 18:50

    In my case Action bar menu was not visible when used public class MainActivity extends Activity { } but was visible when used public class MainActivity extends ActionBarActivity{ }

    0 讨论(0)
提交回复
热议问题