大家好!我目前正在尝试使用导航视图来创建导航抽屉,下面是代码
AndroidMainfest
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".C"
android:label="@string/title_activity_c"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme" >
</activity>
<activity
android:name=".New_Request"
android:label="@string/title_activity_new__request" >
</activity>
</application>
C.java
public class C extends AppCompatActivity {
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
setupDrawerLayout();
// my_child_toolbar is defined in the layout file
Toolbar myChildToolbar = (Toolbar) findViewById(R.id.my_childtoolbar);
setSupportActionBar(myChildToolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
if(ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.rsz_ic_list_white_48dp);
}
}
@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_c, menu);
MenuItem searchitem = menu.findItem(R.id.action_search1);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchitem);
// Define the listener
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when action item collapses
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
};
// Get the MenuItem for the action item
MenuItem actionMenuItem = menu.findItem(R.id.action_sharre);
MenuItemCompat.setOnActionExpandListener(actionMenuItem, expandListener);
// Assign the listener to that action item
return super.onCreateOptionsMenu(menu);
}
private void setupDrawerLayout(){
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_c);
NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Snackbar.make(getWindow().getDecorView(), menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
drawerLayout.closeDrawers();
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.
int id = item.getItemId();
switch (id){
case R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
Log.d("kk","reallt");
return true;
case R.id.action_settings:
return true;
}
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
activity_c.xml
<android.support.v4.widget.DrawerLayout
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"
android:id="@+id/drawer_layout_c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_linear"
tools:context="com.example.ji.myapplication19.C">
<android.support.v7.widget.Toolbar
android:id="@+id/my_childtoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_head"
app:menu="@menu/navigation_view_menu"/>
</android.support.v4.widget.DrawerLayout>
主要问题是:点击汉堡菜单时,它不会出现导航抽屉。 有人帮帮我吗?
答案 0 :(得分:1)
在onCreate()方法
之前的活动类中ActionBarDrawerToggle mDrawerToggle;
在setupDrawerLayout()
中添加:
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, myChildToolbar, "Open", "Close") {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
drawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
在此之后,您还必须对onOptionsItemSelected进行一些更改,如下所示:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:1)
drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);