请帮助将动态内容添加到我现有的导航栏中。这就是我的活动布局现在的样子:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxx.HomeActivity"
android:background="@drawable/stadium_bg"
android:id="@+id/xxx_ayout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/DarkBlue"
app:menu="@menu/drawer_view"
/>
</android.support.v4.widget.DrawerLayout>
NagivationView使用drawer_view在菜单中显示静态内容。所以,drawer_view.xml看起来像这样:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/leagues_in_progress_item"
android:icon="@drawable/ic_format_line_spacing_black_48dp"
android:title="@string/title_activity_leagues_in_progress" />
<item
android:id="@+id/last_matches_item"
android:icon="@drawable/ic_access_alarm_black_48dp"
android:title="@string/title_activity_last_matches" />
<item
android:id="@+id/archive_item"
android:icon="@drawable/ic_folder_open_black_48dp"
android:title="@string/title_activity_archive" />
<item
android:id="@+id/put_score_item"
android:icon="@drawable/ic_add_circle_outline_black_48dp"
android:title="@string/title_activity_put_score" />
</group>
</menu>
我希望将动态项添加到新项目组中,就在静态内容的正下方。以静态方式,这应该像下面解决,但如何动态构建它?
<item android:title="Sub items">
<menu>
<item
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
android:title="Sub item 1" />
<item
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
android:title="Sub item 2" />
</menu>
</item>
在Activity代码中没有什么特别的事情发生。就像这样:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.xxx);
this.mDrawerLayout = (DrawerLayout) findViewById(R.id.pinokioLayout);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
this.mDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout,
R.string.app_name,
R.string.app_name
);
this.mDrawerLayout.setDrawerListener(this.mDrawerToggle);
NavigationView navigationView = (NavigationView)findViewById(R.id.left_drawer);
this.setupDrawerContent(navigationView);
if (savedInstanceState == null) {
this.displayMenuFragment(navigationView.getMenu().getItem(0));
}
}
private void setupDrawerContent(NavigationView navigationView)
{
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
displayMenuFragment(menuItem);
return true;
}
});
}