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 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>
InformationsquelleAutor der Frage Gavriel | 2014-11-04
Du musst angemeldet sein, um einen Kommentar abzugeben.
@Pedro Oliveira Lösung gearbeitet. Ich konnte sogar feststellen das drawable, dass die AppCompat-Bibliothek nutzt (und daher bereits in der apk). Was mehr ist es auch gespiegelt, damit es funktioniert sowohl für ltr, rtl-Gebietsschemas:
und das ist es alles in allem mit der Korrektur von @VictorYakunin
InformationsquelleAutor der Antwort Gavriel
Yout können
?homeAsUpIndicator
stattR.drawable.abc_ic_ab_back_mtrl_am_alpha
:InformationsquelleAutor der Antwort ls.illarionov
Weg zu spät zur party hier, aber ich immer noch meine 0.02$.
ich habe es auf dem folgenden Weg zu zeigen, der Pfeil daneben in
fragment
mitAppCompactActivity
und behandeln Sie das onClick-Ereignis innerhalb Ihrer Tätigkeit
onOptionsItemSelected
InformationsquelleAutor der Antwort war_Hero
Wenn Sie übergeordnete Aktivitäten Namen in
Manifest.xml
- und meta-Daten-Unterstützung von Android 4.0 und niedriger dann können Sie nur setzen Sie das Symbol navigation (toolbar.setNavigationIcon(R.drawable.ic_action_back);
)und es wird funktionieren.
Manifest.xml Beispiel:
InformationsquelleAutor der Antwort Tomasz Mularczyk
Danke für die Lösung, der einzige Punkt ist, ist es besser,
InformationsquelleAutor der Antwort Viktor Yakunin
Definieren Sie einen hintergrund 9patch für Ihre toolbar 😉 es ist Arbeit.
Hier ist toolbar_background.9.png
InformationsquelleAutor der Antwort Stéphane
Versuchen das XML-Attribut android:navigationIcon, oder in der app:navigationIcon für API, die älter als 21.
InformationsquelleAutor der Antwort Xingang Huang