2017-02-23 134 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); 

      } 
     }); 

问候。