我想在一个DrawerLayout
中使用两个Activity
。其中一个在左侧打开,另一个在右侧打开。点击ToolBar
后,它们应该会打开。
例如:如果我点击ToolBar
上的左侧按钮,则会打开左侧DrawerLayout
,如果点击ToolBar
上的右侧按钮,则会打开右侧{{1} }}
请参阅这些图片以了解我的意思。
我的XML代码:
DrawerLayout
java代码:
<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/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/search_hint"
tools:context=".Main_Page">
<include
android:id="@+id/app_bar"
layout="@layout/app_toolbar" />
<com.ogaclejapan.smarttablayout.SmartTabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/app_bar"
android:background="@color/primaryColor"
android:clipToPadding="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:stl_defaultTabTextColor="@color/search_hint"
app:stl_defaultTabTextHorizontalPadding="18dp"
app:stl_defaultTabTextSize="15sp"
app:stl_distributeEvenly="true"
app:stl_dividerColor="@color/transparent"
app:stl_dividerThickness="0dp"
app:stl_indicatorColor="@color/black_20"
app:stl_indicatorCornerRadius="18dp"
app:stl_indicatorGravity="center"
app:stl_indicatorInterpolation="smart"
app:stl_indicatorThickness="36dp"
app:stl_underlineColor="@color/transparent"
app:stl_underlineThickness="0dp" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/viewpagertab">
</android.support.v4.view.ViewPager>
<co.aenterhy.toggleswitch.ToggleSwitchButton
android:id="@+id/toggle"
style="@style/ToggleSwitchButton" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_main__page"
app:headerLayout="@layout/header_drawer_main"
app:itemTextColor="@color/primaryColor"
app:itemIconTint="@color/primaryColor"/>
</android.support.v4.widget.DrawerLayout>
LogCot错误:
toolbar = (Toolbar) findViewById(R.id.app_bar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setText("title");
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
mNaviView = (NavigationView) findViewById(R.id.main_drawer);
mNaviView.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (con.isOnline()) {
new get_menu_info().execute(public_username);
}
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
ImageView e1 = (ImageView) toolbar.findViewById(R.id.right_tool);
e1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawer.openDrawer(Gravity.RIGHT);
}
});
答案 0 :(得分:2)
答案 1 :(得分:2)
对于活动XML,您只需使用.text
添加另一个视图,然后使用与左侧视图相同的方式进行交互。
android:layout_gravity="end"
答案 2 :(得分:1)
您可以使用以下代码打开或关闭抽屉。
/**
* For your right drawer
*/
drawerLayout.openDrawer(Gravity.RIGHT);
drawerLayout.closeDrawer(Gravity.RIGHT);
/**
* For your right drawer
*/
drawerLayout.openDrawer(Gravity.LEFT);
drawerLayout.closeDrawer(Gravity.LEFT);
答案 3 :(得分:1)
假设您的抽屉分别为leftDrawer
和rightDrawer
,您应该可以使用
drawerLayout.openDrawer(leftDrawer);
到左侧抽屉的open,
drawerLayout.closeDrawer(rightDrawer);
打开右侧抽屉
drawerLayout.closeDrawer(leftDrawer);
到左侧抽屉的close,
drawerLayout.closeDrawer(rightDrawer);
分别关闭右侧抽屉。
使用
Drawer.closeDrawer(mNaviView);
和
Drawer.closeDrawer(mNaviView);
另见DrawerLayout Double Drawer (Left and Right Drawers simultaneously)。