ActionBarDrawerToggle 可通过打开的抽屉布局点击
ActionBarDrawerToggle clickable through open drawer layout
我正在尝试实现一个简单的导航抽屉。为此,我使用了 DrawerLayout、ActionBarActivity 和 ActionBarDrawerToggle 的支持库。This is how it currently looks. The problem is that the navigation drawer toggle is clickable when the drawer layout is opened. You can see that in this short youtube video. 我的问题是如何在抽屉布局打开时阻止切换按钮被点击?以下是我
的部分代码
MainActivity.java
...
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.ColorPrimaryDark));
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open, R.string.drawer_close);
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
...
}
以及此 activity
的布局
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Your main content -->
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"></include>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
<!-- The navigation drawer -->
<com.username.navigationdrawertemplate.ScrimInsetsFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrimInsetsFrameLayout"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/ColorDrawerBackground"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000">
</com.username.navigationdrawertemplate.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
将android:clickable="true"
放入导航抽屉布局
我正在尝试实现一个简单的导航抽屉。为此,我使用了 DrawerLayout、ActionBarActivity 和 ActionBarDrawerToggle 的支持库。This is how it currently looks. The problem is that the navigation drawer toggle is clickable when the drawer layout is opened. You can see that in this short youtube video. 我的问题是如何在抽屉布局打开时阻止切换按钮被点击?以下是我
的部分代码MainActivity.java
...
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.ColorPrimaryDark));
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open, R.string.drawer_close);
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
...
}
以及此 activity
的布局<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Your main content -->
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"></include>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
<!-- The navigation drawer -->
<com.username.navigationdrawertemplate.ScrimInsetsFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrimInsetsFrameLayout"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/ColorDrawerBackground"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000">
</com.username.navigationdrawertemplate.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
将android:clickable="true"
放入导航抽屉布局