2015-09-11 286 views
0

所以我有一箇舊的應用程序。 我想添加一個導航抽屜。 所以我想導航抽屜與我目前的工作活動,並只顯示在currecnt活動導航抽屜沒有顯示

public class WebViewActivity extends Activity { 

所以主要活動是網頁視圖。

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

因此,我希望當我點擊菜單按鈕或web視圖我檢測手勢從左到右

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);沒有做任何事情。 日誌中沒有錯誤。 已插入的抽屜不顯示,沒有任何反應。 我遵循official指南和更多在線。沒有運氣。

我不想使用碎片!

UPDATE

的問題是,在我的 '的onCreate' 我有類似:setContentView(webView);

所以,如果我刪除它,我的導航抽屜的作品! 但我的webView沒有。 我錯過了什麼?

回答

1

從main_activity刪除的FrameLayout然後嘗試

+1

你能否詳細說明一下? – Kmeixner

+1

DrawerLayout應該有兩個直接的子視圖,第一個用於主要內容,第二個用於導航抽屜。在你的情況下,你有三個相對佈局,然後是一個框架佈局,然後是一個ListView。你似乎沒有使用任何框架佈局,所以刪除它,那麼你將有兩個,像Drawer Layout期望的那樣。 –

+0

剛剛做了,輸出相同:沒有任何反應。 – AAlferez

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

只需複製並粘貼

0

你的onCreate()必須有setContentView(R.layout.drawer_layout); 其中drawer_layout是具有DrawerLayout作爲其根視圖中的XML

按照documentation 下載示例代碼,看看哪個x毫升傳遞到setContentView()

如果你仔細關注它,你會注意到activity_main.xml包含Drawerlayout而不是主要內容的xml。

而且記住,DrawerLayout必須包含兩個子ViewGroup s,其中第一個可以在運行時填充一個片段一個片段或的FrameLayout和第二必須是NavigationView

如果你仍然困惑,你可以請按照this guide的說明進行分步操作。它值得一遊。