简体   繁体   中英

Home button is not shown in action bar

This is first time i make a new apps since the time Action Bar was enabled in android 2.xx by using android support ver7. Today i just install Android Studio (Because eclipse is no longer official tools for android development) and using it to make my apps. I notice that Home button is not show on the action bar when i run the app.

I already have these line in my code :

    mainActionBar = getSupportActionBar();
    mainActionBar.setCustomView(R.layout.search_bar);
    mainActionBar.setHomeButtonEnabled(true);
    mainActionBar.setDisplayShowCustomEnabled(true);
    mainActionBar.setDisplayOptions(DISPLAY_SHOW_HOME| DISPLAY_SHOW_TITLE| DISPLAY_SHOW_CUSTOM);

But Home button is not display on the corner of the action bar. Then i try too add these line:

    mainActionBar.setDisplayShowHomeEnabled(true);
    mainActionBar.setIcon(R.drawable.ic_launcher);

The icon launcher is display but i can press on it.

Is there anybody can help me to solve it ? In my older apps, I used ActionbarSherlock and everything worked fine. Here is the code in my older apps:

    _action = getSupportActionBar();
    _action.setCustomView(R.layout.search_bar);
    _action.setDisplayShowCustomEnabled(true);
    _action.setHomeButtonEnabled(true);
    _action.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);

You can Use this

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Hope this Helps !

Make sure you have the android.support.v7.widget.Toolbar in your layout.

Let's say it has id "toolbar". Then in activity, fetch it and add your preferred settings.

    // onCreate method of activity:
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM