Action bar not shown in Android Studio

前端 未结 3 1447
生来不讨喜
生来不讨喜 2021-01-27 19:48

I\'m using Android Studio for the first time and can\'t manage to make the ActionBar show when I run the app. Here\'s what I have so far:

My Activity Ma

3条回答
  •  不要未来只要你来
    2021-01-27 20:26

    Use AppCompatActivity with ToolBar as per Material Design.

    public class MyActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
            // Find the toolbar view inside the activity layout
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            // Sets the Toolbar to act as the ActionBar for this Activity window.
            // Make sure the toolbar exists in the activity and is not null
            setSupportActionBar(toolbar);
        }
    
        // Menu icons are inflated just as they were with actionbar
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    }
    

    activity_my.xml

    
    
        
        
    
        
    
    
    

    Add this in Style.xml.

    
    
    
    
    
    
    
    

    menu.xml

    
        
        
        
        
    
    

提交回复
热议问题