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