So aktivieren Sie homeAsUp oder rufen Sie setDisplayHomeAsUpEnabled() auf standalone-toolbar mit appcompat v21

Ich versuche zu konvertieren, meine app zu verwenden, die v21 AppCompat-Bibliothek, so dass ich anfing zu verwenden Symbolleiste statt der ActionBar. In allen meinen regelmäßigen Aktivitäten (die sich ActionBarActivity) alles in Ordnung ist. aber in meinem SettingsActivity die extends PreferenceActivity, und deshalb kann ich nicht verwenden, die setSupportActionBar(actionbar) nenne ich eine "standalone" - Symbolleiste. Die Symbolleiste zeigt nach oben, aber ich kann nicht herausfinden, wie könnte ich hinzufügen "home /nach oben" - Taste auf der Symbolleiste.

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>

Habe ich versucht, hinzuzufügen actionBarStyle und displayOptions auf mein Thema, wie in Show up - /zurück-Taste auf android-verschachtelte PreferenceScreen?sondern auch in anderen Orten die Menschen sagten, dass actionBarStyle nicht verwendet werden, wenn ich verwenden Sie die Symbolleiste, und Sie scheinen Recht zu sein.

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 AppCompats actionBarStyle -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

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

InformationsquelleAutor der Frage Gavriel | 2014-11-04

Schreibe einen Kommentar