当我通过滑动打开抽屉时,我正在使用Navigation Drawer
并且ActionBarDrawerToggle
是同步的,但是当我点击Toolbar
图标时,它不会打开抽屉。
我的代码有什么问题。
actionBarDrawerToggle = new ActionBarDrawerToggle(Main2Activity.this, drawerLayout, toolbar, R.string.drawer_open, R.string.close_drawer) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
drawerLayout.post(new Runnable() {
@Override
public void run() {
actionBarDrawerToggle.syncState();
}
});
我为此尝试的事情:
actionBar.setDisplayHomeAsUpEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(Gravity.START);
return true;
}
return super.onOptionsItemSelected(item);
}
这是我使用的布局。我有NavigationView
和抽屉布局。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Main2Activity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar"
app:theme="@style/ToolbarColoredIcon" />
<include layout="@layout/content_main2" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<LinearLayout
android:id="@+id/nav_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--header-->
<include
android:id="@+id/header"
layout="@layout/nav_header"
/>
<!--items-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/dp20">
<include
android:id="@+id/nav_menue_item_home"
layout="@layout/nav_menue_item" />
<include
android:id="@+id/nav_menue_item_settings"
layout="@layout/nav_menue_item" />
<include
android:id="@+id/nav_menue_item_more_apps"
layout="@layout/nav_menue_item" />
<include
android:id="@+id/nav_menue_item_rate_us"
layout="@layout/nav_menue_item" />
<include
android:id="@+id/nav_menue_item_privacy_policy"
layout="@layout/nav_menue_item" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e3e4e3"
android:gravity="bottom"
android:orientation="vertical">
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adViewNative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
ads:adSize="@string/native_sm_ad_size"
ads:adUnitId="@string/native_sm_ad_unit_id" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
所以请问,现在问题在哪里?
感谢所有我解决了我的问题。问题实际上不在代码中,但问题出现在布局中,我添加了一个不允许工具栏点击的TabHost,这是实际问题。
答案 0 :(得分:1)
创建toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/toolbar"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
>
</android.support.v7.widget.Toolbar>
创建drawer_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single"
android:id="@+id/first_group">
<item
android:id="@+id/first_item"
android:title="First Item"
android:icon="@drawable/first_item"
/>
</group>
<group
android:checkableBehavior="single"
android:id="@+id/second_group">
<item
android:id="@+id/second_item"
android:title="Second Item"
android:icon="@drawable/second_item">
</item>
</group>
</menu>
然后你的活动格式应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<!--First child represents elements of activity in drawerLayout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/relative_layout">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
/>
</RelativeLayout>
<!--Second child (navigation view) represents elements of navigation drawer in drawerLayout-->
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="start"
android:background="@color/toolbar"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
汉堡图标:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
然后创建一个方法:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
如果有帮助,请告诉我。