我有一个应用程序,将默认活动显示为:
点击后,汉堡包图标会打开左侧主导航栏:
点击后,FILTER按钮从右侧打开另一个抽屉:
我知道左侧导航抽屉的显示方式(所有java代码和布局)
显示从右侧打开的抽屉的代码是什么?
我也想知道,即使FILTER按钮位于工具栏之外,如何设置on click侦听器?
有人可以帮忙吗?
答案 0 :(得分:1)
只需将tools:openDrawer
的值更改为结束(即 tools:openDrawer="end"
),并在android:layout_gravity="end"
中添加<android.support.design.widget.NavigationView>
。这是解决方案。
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
<强> MainActivity.java 强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
//Add this piece of code
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(drawer.isDrawerOpen(Gravity.RIGHT)){
drawer.closeDrawer(Gravity.RIGHT);
}else{
drawer.openDrawer(Gravity.RIGHT);
}
}
});
}
答案 1 :(得分:1)