How to enable homeAsUp or call setDisplayHomeAsUpE

2019-01-21 05:53发布

I am trying to convert my app to use the v21 AppCompat library, so I started to use Toolbar instead of ActionBar. In all my regular activities (which extend ActionBarActivity) everything is fine. but in my SettingsActivity which extends PreferenceActivity, and therefore I can't use the setSupportActionBar(actionbar) call I need to use a "standalone" toolbar. The toolbar shows up, but I can't figure out how could I add the "home / up" button to the toolbar.

SettingsActivity.java:

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar);
        if (null != actionbar) {

            // In every other activity that extends ActionBarActivity I simply use:
            // setSupportActionBar(actionbar);
            // final ActionBar supportActionBar = getSupportActionBar();
            // supportActionBar.setDisplayHomeAsUpEnabled(true);

            // but in PreferenceActivity you need to add a standalone toolbar:    
            actionbar.setTitle(R.string.title_activity_settings);
            actionbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SettingsActivity.this.finish();
                }
            });

            // Inflate a menu to be displayed in the toolbar
            actionbar.inflateMenu(R.menu.settings);
        }
    }
}

layout/activity_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              tools:context=".SettingsActivity"
              tools:menu="settings"
              tools:actionBarNavMode="standard"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <include
        layout="@layout/actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list" />
</LinearLayout>

layout/actionbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/actionbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark"
    app:theme="@style/AppTheme"
    />

menu/settings.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.fletech.android.redalert.SettingsActivity" >
</menu>

I tried to add actionBarStyle and displayOptions to my theme as explained in Show up/back button on android nested PreferenceScreen?, but in other places people said that actionBarStyle won't be used when I use Toolbar, and they seem to be right.

values/themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppTheme.Base"/>

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

        <!-- Set AppCompat’s actionBarStyle -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
        <item name="displayOptions">showHome|homeAsUp|showTitle</item>
    </style>
<resources>

7条回答
再贱就再见
2楼-- · 2019-01-21 05:57

Yout can use ?homeAsUpIndicator instead of R.drawable.abc_ic_ab_back_mtrl_am_alpha:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:navigationIcon="?homeAsUpIndicator"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/title"
        />
查看更多
Evening l夕情丶
3楼-- · 2019-01-21 05:59

way to late to the party here but i will still provide my 0.02$. i did it the following way to show the up arrow mark in fragment using AppCompactActivity

((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and handle the onClick event inside your activity onOptionsItemSelected

查看更多
太酷不给撩
4楼-- · 2019-01-21 05:59

If you provide parent activity name in Manifest.xml and meta-data to support Android 4.0 and lower then you can just set navigation icon (toolbar.setNavigationIcon(R.drawable.ic_action_back);) and it will work.

Manifest.xml example:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ProductActivity"
        android:label="@string/title_activity_product"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
查看更多
家丑人穷心不美
5楼-- · 2019-01-21 06:00

Define a background 9patch for your toolbar ;) it's work. Here is toolbar_background.9.png

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

    .....
查看更多
Deceive 欺骗
6楼-- · 2019-01-21 06:04

Try XML attribute android:navigationIcon, or app:navigationIcon for API older than 21.

查看更多
做个烂人
7楼-- · 2019-01-21 06:05

Thank you for the solution, the only point to add is that is better to use

NavUtils.navigateUpFromSameTask(SettingsActivity.this); 
查看更多
登录 后发表回答