[英]DrawerLayout navigation drawer not displayed
我正在嘗試在工具欄中顯示導航抽屜。 我認為我已經編寫了所需的一切,但工具欄中沒有顯示任何內容。 只有文字動物園 。 不是顯示導航欄的圖標
我是android新手,如果我錯過了某些東西,我不會感到驚訝。
這是我的MainActivity.java
package com.example.zoo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_Layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.drawer_opened);
}
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.drawer_closed);
}
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(mActionBarDrawerToggle.onOptionsItemSelected(item))
return true;
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這是我的content_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gsiradze.zoo.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
當然我有這些字符串名稱
<resources>
<string name="drawer_opened">Select an item</string>
<string name="drawer_closed">Zoo</string>
</resources>
問題是這一行:
getSupportActionBar().setDisplayShowHomeEnabled(true);
對於您當前的設置,它應該是:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
您也可以使用Toggle的五參數構造函數,並將Toolbar
對象作為第三個參數傳遞。
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_opened,
R.string.drawer_closed) {...};
在這種情況下,您可以省略支持ActionBar
set*()
調用,並在Toggle上檢查onOptionsItemSelected(item)
。 如果您的Toolbar
未設置為支持ActionBar
這將非常有用。 但是,如果Toolbar
設置如此,則文檔建議使用四參數構造函數,從而推薦當前設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.