繁体   English   中英

导航抽屉按钮单击

[英]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

有谁知道如何修理它? 我想要的是单击imagebutton (mDrawerToggle)时将打开导航抽屉

谢谢

您不能在片段中编写findViewById来查找活动xml视图。

在您的活动中使这个私人成员成为现实

DrawerLayout mDrawerLayout,drawer;

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

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

制作一种方法以获取活动中DrawerLayout的引用

 public DrawerLayout getDrawerLayout(){
   return mDrawerLayout; 
   }

 public DrawerLayout getDrawerLayout(){
   return drawer; 
   }

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM