单击按钮时的导航抽屉

Navigation Drawer On Button Click

我想在我的应用程序中有一个导航抽屉

我已经做了这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <View
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="#7e7e7e" />

    <WebView
        android:id="@+id/sitesWebView"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.07" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFDFE"
        android:minHeight="?attr/actionBarSize">

        <ImageView
            android:id="@+id/webviewBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:background="@drawable/imageview_selector"
            android:padding="5dp"
            android:src="@drawable/ic_back" />

        <ImageView
            android:id="@+id/webviewForward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:layout_toRightOf="@+id/webviewBack"
            android:background="@drawable/imageview_selector"
            android:padding="5dp"
            android:src="@drawable/ic_forward" />

        <ImageView
            android:id="@+id/mDrawerToggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/imageview_selector"
            android:padding="5dp"
            android:src="@drawable/ic_drawer" />

    </RelativeLayout>


</LinearLayout>

我想使用这个图片按钮

<ImageView
    android:id="@+id/mDrawerToggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/imageview_selector"
    android:padding="5dp"
    android:src="@drawable/ic_drawer" />

单击时能够打开导航抽屉

我制作片段

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.webviewBack:
            isWebViewCanGoBack();
            break;
        case R.id.webviewForward:
            if (webView.canGoForward())
                webView.goForward();
            break;
        case R.id.webviewReload:
            String url = webView.getUrl();
            LoadWebViewUrl(url);
            break;
        case R.id.webviewClose:
            finish();
            break;
        case R.id.mDrawerToggle:
            DrawerLayout mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.mDrawerToggle);
            final DrawerLayout drawer = (DrawerLayout)getActivity().findViewById(R.id.mDrawerToggle);
            drawer.openDrawer(Gravity.LEFT);
            return false;
    }
}

但我会得到错误

error: cannot find symbol method getActivity()
error: incompatible types: unexpected return value

有人知道怎么解决吗? 我想要的是当图像按钮(mDrawerToggle)被点击时它会打开导航抽屉

谢谢

你不能在片段中写 findViewById 来查找 activity xml views.Instead 试试这个

在你activity让这个私人会员成为virailbe

DrawerLayout mDrawerLayout,drawer;

然后在onCreate中使用findViewById找到你的抽屉。

mDrawerLayout = (DrawerLayout)findViewById(R.id.mDrawerToggle);
drawer = (DrawerLayout)findViewById(R.id.mDrawerToggle);

在你的 activity

中创建一个获取 DrawerLayout 引用的方法
 public DrawerLayout getDrawerLayout(){
   return mDrawerLayout; 
   }

 public DrawerLayout getDrawerLayout(){
   return drawer; 
   }

然后在您的片段中像这样调用您的 activity 方法

DrawerLayout drawer=((YOURACTIVITY)getActivity()).getDrawerLayout();