简体   繁体   English

导航抽屉按钮单击

[英]Navigation Drawer On Button Click

I want to have a navigation drawer in my app 我想在我的应用程序中有一个导航抽屉

I have already make this 我已经做过了

<?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>

and i want to use this image button 我想使用此图像按钮

<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" />

to be able open the navigation drawer when it's click 单击时能够打开导航抽屉

I make the fragment 我做碎片

@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;
    }
}

but i'll get error 但我会出错

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

does anyone know how to fix it? 有谁知道如何修理它? what i want is when the imagebutton (mDrawerToggle) is clicked it'll open navigation drawer 我想要的是单击imagebutton (mDrawerToggle)时将打开导航抽屉

thanks 谢谢

You cannot write findViewById in fragment to find activity xml views.Instead try this 您不能在片段中编写findViewById来查找活动xml视图。

In you activity make this private memeber virailbe 在您的活动中使这个私人成员成为现实

DrawerLayout mDrawerLayout,drawer;

Then in onCreate find your drawer using findViewById . 然后在onCreate使用findViewById找到您的抽屉。

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

Make a method to get the reference of DrawerLayout in your activity 制作一种方法以获取活动中DrawerLayout的引用

 public DrawerLayout getDrawerLayout(){
   return mDrawerLayout; 
   }

 public DrawerLayout getDrawerLayout(){
   return drawer; 
   }

And in your fragment call your activity method like this 然后在片段中像这样调用您的活动方法

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

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

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