【发布时间】:2018-03-02 07:44:52
【问题描述】:
我想在我的应用中有一个抽屉式导航
我已经做了这个
<?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) 被点击它会打开导航抽屉
谢谢
【问题讨论】:
-
你可以写 getApplicationContext() 或 getParentActivity() 代替 getActivity()。
标签: android android-studio navigation drawer