2017-02-23 137 views
0

沒有行動酒吧,我​​創建了抽屜菜單: drawer menu. 正如你所看到的,我設法完美展現抽屜,但是當我包括activity_main.xml中它沒有得到的數據(變量&佔位符)抽屜導航對於現有項目

我main_activity.xml:how it looks, TextView should be numbers and placeholder not getting any data 所以... drawer_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:openDrawer="end" 
> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 
    <include 
     layout="@layout/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/main_container" 
    /> 

    <android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="end" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/drawer_menu" /> 
    </android.support.v4.widget.DrawerLayout> 

我需要顯示的數據,並仍採用抽屜......這可能嗎?如果你需要更多的信息,只是問,我會提供

編輯:我需要將信息從MainActivity.class(連接到activity_main.xml)傳遞到Drawer_layout.class? (連接到drawer_layout.xml)

回答

0

沒關係的傢伙,我設法做到這一點我自己。如果其他人有這個問題: 包含標籤是的,它不顯示或使用您的活動(在我的情況下,MainActivity.class),所以我所做的是將activity_main.xml從RelativeLayout更改爲DrawerLayout,不需要分離層。 你可以看到:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:background="@color/background" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/mainactivity" 
android:paddingLeft="@dimen/mainactivity" 
android:paddingRight="@dimen/mainactivity" 
android:paddingTop="@dimen/mainactivity" 
android:fitsSystemWindows="true" 
tools:openDrawer="end" 
tools:context=".MainActivity"> 

<!-- app code --> 
<LinearLayout...> 

<!-- navigation drawer --> 
<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    app:menu="@menu/navigation_menu" 
    android:layout_gravity="end" 
    android:fitsSystemWindows="true" 
    ads:headerLayout="@layout/navigation_header" 
    > 

</android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout> 

它完美的作品,如果你有一個現有的項目,你需要添加導航的抽屜裏。手動完成整個事情,比嚮導更好。

然後在MainActivity.class我指的導航像往常一樣

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_info) { 

     startActivity(aboutact); 

    } else if (id == R.id.nav_appinfo) { 

     startActivity(appinfact); 

    } else if (id == R.id.nav_contact) { 

     startActivity(contactact); 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_main); 
    drawer.closeDrawer(GravityCompat.END); 
    return true; 
} 

當然,你仍然需要在navigation_menu.xml menu pacakge ...

,我不喜歡使用動作條,你可以看,沒有工具欄,什麼都沒有。所以我從點擊按鈕開始抽屜:

mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main); 
    hamburgerbutton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mDrawerLayout.openDrawer(GravityCompat.END); 

      } 
     }); 

問候。