Eine mögliche Lösung
Aktivität:
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
{
DrawerLayout drawerLayout;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toggle = new ActionBarDrawerToggle
(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
{
};
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (toggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
Layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/list_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f2f7"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent" />
</android.support.v4.widget.DrawerLayout>
Stil:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
Es ist wichtig, dass Ihre App das AppCompat Thema erben.
Wenn Sie die ActionBar durch eine Symbolleiste ersetzt nicht vergessen, die ActionBar zurück zu setzen, indem Sie diese Zeile in der styles.xml Entfernung:
<item name="windowActionBar">false</item>
Gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:22.1.1'
compile 'com.android.support:appcompat-v7:22.1.1'
}
habe ich diesen Code auf GitHub: https://github.com/bbouabou/AppCompatActivity-With-ActionBarDrawerToggle.
Aus Neugier - wer sagt das * "wir brauchen nicht mehr eine Toolbar-Ansicht in unserem Aktivitätslayout" *? Ich kann keine Referenz dafür finden. – reVerse
Die neue 'AppCompatActivity' verfügt bereits über eine Symbolleiste (von der ich glaube, dass sie entfernt/ausgeblendet werden kann), sodass Sie keine weitere hinzufügen müssen. – Guilherme
Die' AppCompatActivity' enthält standardmäßig eine 'ActionBar'. Sie müssen das Theme immer noch in 'Theme.AppCompat.NoActionBar' ändern und Ihrem Layout eine' Toolbar' hinzufügen. – reVerse