Action bar not shown in Android Studio

前端 未结 3 1316
生来不讨喜
生来不讨喜 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:09

    In the design view, click the eye icon (view options) and select "Show Layout Decorations"

    0 讨论(0)
  • you must call you actionBar almost similar to how you would inflate a view and remember to use tool bar

    Toolbar toolbar = (Toolbar)this.findViewById(R.id.action_bar);
          if(toolbar!=null){
    
          }
          //getSupportActionBar().setHomeButtonEnabled(true);
          getSupportActionBar().setDisplayShowHomeEnabled(true);
          getSupportActionBar().setDisplayUseLogoEnabled(true);
    
          getSupportActionBar().setLogo(R.drawable.ic__actionbar);
    
          // Set up the action bar.
          final ActionBar actionBar = getSupportActionBar();
          if (showTabs) actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
          // Create the adapter that will return a fragment for each of the three
          // primary sections of the activity.
    
    0 讨论(0)
  • 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

    <LinearLayout 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="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:minHeight="?attr/actionBarSize"  
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:titleTextColor="@android:color/white"
          android:background="?attr/colorPrimary">
        </android.support.v7.widget.Toolbar>
    
        <!-- Layout for content is here. This can be a RelativeLayout  -->
    
    </LinearLayout>
    

    Add this in Style.xml.

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <!-- Customize your theme here. -->
      <item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
    </style>
    
    <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      <!-- This would set the toolbar's background color -->
      <item name="android:background">@color/colorPrimary</item>
      <!-- android:textColorPrimary is the color of the title text in the Toolbar  -->
      <item name="android:textColorPrimary">@android:color/holo_blue_light</item>
      <!-- android:actionMenuTextColor is the color of the text of action (menu) items  -->
      <item name="actionMenuTextColor">@android:color/holo_green_light</item>
      <!-- Enable these below if you want clicking icons to trigger a ripple effect -->
      <!-- 
      <item name="selectableItemBackground">?android:selectableItemBackground</item>
      <item name="selectableItemBackgroundBorderless">?android:selectableItemBackground</item> 
      -->
    </style>
    
    <!-- This configures the styles for the title within the Toolbar  -->
    <style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">21sp</item>
        <item name="android:textStyle">italic</item>
    </style>
    

    menu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/miCompose"
            android:icon="@drawable/ic_compose"
            app:showAsAction="ifRoom"
            android:title="Compose">
        </item>
        <item
            android:id="@+id/miProfile"
            android:icon="@drawable/ic_profile"
            app:showAsAction="ifRoom|withText"
            android:title="Profile">
        </item>
    </menu>
    
    0 讨论(0)
提交回复
热议问题