Android: How to/Tutorial for changing ActionBar to ActionBarCompat (Toolbar)?

前端 未结 3 1329
夕颜
夕颜 2020-12-13 22:33

I\'ve been stuck on this, checked the official guidance, etc. Any tutorials/what are the steps to change from ActionBar to ActionBarCompat (for the Toolbar and support of ol

相关标签:
3条回答
  • 2020-12-13 23:09

    I've learned with my application is that you can't use the toolbar and the actionbar at the same time. So that might be where your issue lies, but I'm not sure because you didn't post your code, but using the styles.xml file you have to specify either or like so:

    <style name="AppTheme.Base" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/mycolorprimary</item>
        <item name="colorPrimaryDark">@color/mycolorprimarydark</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
    

    Activity.xml:

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- dont use this if you only want to use the actionbar instead -->
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />
    
    </LinearLayout>
    

    Activity.java:

        public class MyActivity extends ActionBarActivity {
    
           Toolbar mToolbar;
    
           public void onCreate(Bundle savedInstanceState) {
    
              // configure toolbar stuff
              setSupportActionBar(mToolbar);
    
              // or if you don't want to use the toolbar
              // then change the style values accordingly
              // and then you can run getSupportActionBar() instead
    
           }
    
        }
    

    Reference: here .. hope this helps!

    0 讨论(0)
  • 2020-12-13 23:09

    @ Andrew Butler: good answer, though there is a standard way to not use an actionbar.

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here.>
    </style>
    
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here.>
    </style>
    
    0 讨论(0)
  • 2020-12-13 23:21

    Set the parent of your theme to one without an action bar. For example

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/mycolorprimary</item>
        <item name="colorPrimaryDark">@color/mycolorprimarydark</item>
        <item name="android:windowNoTitle">true</item>
    </style>
    

    There would be no need to set windowActionBar to false anymore.

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