@H_404_1@我正在尝试在工具栏中显示导航抽屉.我认为我已经编写了所需的一切,但工具栏中没有显示任何内容.只有文字动物园.不是显示导航栏的图标
@H_404_1@我是android新手,如果我错过了某些东西,我不会感到惊讶.
@H_404_1@这是我的MainActivity.java
@H_404_1@
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);
}
}
@H_404_1@这是我的content_main.xml
@H_404_1@
当然我有这些字符串名称
最佳答案
问题是这一行:
getSupportActionBar().setDisplayShowHomeEnabled(true);
对于您当前的设置,它应该是:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
您也可以使用Toggle的五参数构造函数,并将Toolbar对象作为第三个参数传递.
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,toolbar,R.string.drawer_closed) {...};
在这种情况下,您可以省略支持ActionBar set *()调用,并在Toggle上检查onOptionsItemSelected(item).如果您的工具栏未设置为支持ActionBar,这将非常有用.但是,如果工具栏设置如此,则documentation建议使用四参数构造函数,从而推荐当前设置.