![](/img/trans.png)
[英]menu layout for the navigation menu is not showing up in the navigation drawer
[英]Navigation Drawer not showing up
所以我有一個舊的應用程序。 我想添加一個導航抽屜。 因此,我希望導航抽屜與我當前的活動配合使用,並且僅在當前活動上顯示
public class WebViewActivity extends Activity {
因此,主要活動是一個webView。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
main content
-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</RelativeLayout>
<!--
navigation list item
-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#2A323D"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
因此,我希望當我單擊菜單按鈕或WebView從左到右檢測到手勢時,抽屜會出現
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
drawerView = (View)findViewById(R.id.left_drawer);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
和
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE) {
mDrawerLayout.openDrawer(GravityCompat.START);
return false;
} else {
// consider as something else - a screen tap for example
}
break;
}
return true;
}
});
我真的不知道我缺少什么,但是mDrawerLayout.openDrawer(GravityCompat.START); 什么也沒做 日志中沒有錯誤。 假定的抽屜沒有出現,什么也沒發生。 我遵循了官方指南以及更多在線內容。 沒運氣。
我不想使用碎片!
更新
問題是,在我的“ onCreate”中,我有類似以下內容: setContentView(webView);
因此,如果我刪除它,我的導航抽屜將起作用! 但是我的webView沒有。 我想念什么?
從main_activity中刪除FrameLayout,然后嘗試
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</RelativeLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#2A323D"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
只需復制並粘貼
您的onCreate()必須具有setContentView(R.layout.drawer_layout);
其中抽屜式布局是具有DrawerLayout
作為其根視圖的xml
按照文檔 下載示例代碼,查看將哪個xml傳遞給setContentView()
如果仔細遵循,您會注意到activity_main.xml包含Drawerlayout而不是主要內容的xml。
還要記住, DrawerLayout
必須恰好包含兩個子ViewGroup
,其中第一個可以是片段或在運行時填充一個片段的FrameLayout,而第二個必須是NavigationView
如果您仍然感到困惑,可以按照本指南進行操作 ,其中包含分步說明。 它值得一游。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.