What is the alternative to getActionBar()?

后端 未结 2 1677
小蘑菇
小蘑菇 2020-12-21 22:49

When I try to use getActionBar() it says it is deprecated, so that means by going forward we are not allowed to use getActionBar().

Which

相关标签:
2条回答
  • 2020-12-21 23:21

    If you are using appcompat, the correct method is getSupportActionBar()

    0 讨论(0)
  • 2020-12-21 23:28

    The reason you should create this, because Fragment Tabs using ActionBar is now deprecated. This is new way to create your actionbar with Material Design. to use this you need to add dependencies in your build.gradle file.

    compile 'com.android.support:appcompat-v7:21.0.0'
    

    create toolbar app_bar.xml file, you can give the name you want.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
    

    Make sure, you selected theme in styles.xml with

    "Theme.AppCompat.NoActionBar"
    

    after that, you need to initialize this toolbar in your mainActivity.xml layout file, for that include this,

     <include
            android:id="@+id/app_toolbar"
            layout="@layout/app_toolbar"/>
    

    then in the MainActivity, you need to make instance of Toolbar toolbar. then, after setContentView() add following with your

     toolbar = (Toolbar) findViewById(R.id.app_toolbar);
        setSupportActionBar(toolbar);
    
    0 讨论(0)
提交回复
热议问题