How to change HomeAsUp indicator in new AppCompat Toolbar?

半世苍凉 提交于 2019-11-29 00:07:27

问题


I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that? Only default arrow shows up.

styles (same for other API's level):

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- API 14 theme customizations can go here. -->
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:homeAsUpIndicator">@drawable/home</item>

        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>
        <item name="homeAsUpIndicator">@drawable/home</item>
</style>

Toolbar in my fragment layout:

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:homeAsUpIndicator="@drawable/home"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/AppTheme" />

In Fragment:

toolbar = (Toolbar) rootView.findViewById(R.id.my_awesome_toolbar);

        activity.setSupportActionBar(toolbar);

        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

回答1:


To change icon just call at runtime:

toolbar.setNavigationIcon(R.drawable.home);

Trick with styles/themes not working.

How to enable homeAsUp or call setDisplayHomeAsUpEnabled() on standalone toolbar with appcompat v21




回答2:


I tried setHomeAsUpIndicator(int resId) ,it works.

private void initToolbar ( Toolbar mToolbar ) {

    mToolbar.setTitleTextColor ( getResources ().getColor ( R.color.main_title ) );
    setSupportActionBar ( mToolbar );

    ActionBar actionbar = getSupportActionBar ();
    actionbar.setDisplayHomeAsUpEnabled ( true );
    actionbar.setHomeAsUpIndicator ( R.drawable.ic_action_back );
}

But mToolbar.setNavigationIcon(R.drawable.ic_action_back) doesn't work :(




回答3:


Just add this line to your styles.xml

        <item name="navigationIcon">@drawable/ic_back_arrow</item>



回答4:


If android.support.v7.app.ActionBarDrawerToggle used together with DrawerLayout and Toolbar you can change homeAsUp icon with the following code:

//set home as up indicator
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_up_indicator);

//remove home as up indicator
mDrawerToggle.setHomeAsUpIndicator(null);

to show homeAsUpIndicator indicator instead of home indicator do following:

mDrawerToggle.setDrawerIndicatorEnabled(false);

Docs:

ActionBarDrawerToggle#setHomeAsUpIndicator ActionBarDrawerToggle#setDrawerIndicatorEnabled




回答5:


i just found out that it actually works toolbar.setNavigationIcon(R.drawable.ic_action_back) and here is the code to make it work.

  @Override
   protected void onCreate(Bundle savedInstanceState) {

   .....
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_action_back);

Post the following code in onOptionsItemSelected(MenuItem item)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   ......
    if (id == android.R.id.home) {
        startActivity(new Intent(this, MainActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps




回答6:


Unfortunately, if this style line is used in a commonly shared AppCompat ToolBar/App bar with other activities in the App, including the main activity, the "navigationIcon" specified will be automatically shown, unlike the standard default HomeAsUpIndicator, which is not shown unless explicitly enabled as desired in an Activity, typically as follows: (in the onCreate())

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

With the above style line, since it behaves in an opposite manner, in a similar fashion, the indicator needs to be explicitly disabled if it is not desired to be shown, as on the main activity, as follows: (in the onCreate())

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

(as observed on Android 4.0.4, 4.3, and 4.4.2 phones)



来源:https://stackoverflow.com/questions/26778278/how-to-change-homeasup-indicator-in-new-appcompat-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!