“Back button” using getSupportActionbar and appcompat v7 toolbar

前端 未结 8 593
时光取名叫无心
时光取名叫无心 2021-01-30 02:27

I\'m using the new toolbar from the Appcompat V7 library and I\'m making an application with navigation drawer and with fragments.

In some fragments I don\'t want to sho

相关标签:
8条回答
  • 2021-01-30 03:05

    in manifest add these lines under the activity you want the back arrow working

    android:parentActivityName="Your parent activity name"

    0 讨论(0)
  • 2021-01-30 03:10

    1- Create Toolbar layout;

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/dark_blue"
        android:minHeight="?attr/actionBarSize"
        local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    

    2- Include this in your layout at the place where you want the toolbar to be.

    3- Paste the following code in your activity.(extends ActionBarActivity)

    private Toolbar mToolbar;
    
    //For Toolbar (Action bar) start
            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            mToolbar.setNavigationIcon(R.drawable.ic_back_arrow);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
            getSupportActionBar().setTitle("Event Details");
            //For Toolbar (Action bar) end
    

    4- change the back click icon to whatever you want.

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