点击Android

时间:2015-10-14 15:13:25

标签: android android-layout

我想在一个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);
    }
});

enter image description here enter image description here

4 个答案:

答案 0 :(得分:2)

您应该将以下内容放入工具栏图标的点击监听器中:

drawerLayout.openDrawer(GravityCompat.START);

有关详细信息,请参阅docs

答案 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)

假设您的抽屉分别为leftDrawerrightDrawer,您应该可以使用

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)