我尝试按照this回答,主要想法是覆盖setContentView
中的BaseActivity
,这项活动将由所有活动扩展,因此{{1}将有导航抽屉。
但是,在我尝试实施答案后,我的导航从未显示(即使在BaseActivity
中)。这就是我所做的:
这是导航抽屉的xml:
BaseActivity
这是我的主调<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" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
(在setContentView
中):
BaseActivity
这是我@Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); // Your base layout here
actContent= (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
//listLayout = (LinearLayout) mDrawerLayout.findViewById(R.id.listLayout);
getLayoutInflater().inflate(layoutResID, actContent, true);
//getLayoutInflater().inflate(layoutResID, listLayout, true);
super.setContentView(layoutResID);
}
上的onCreate
,请注意我使用BaseActivity
代替getLayoutInflater().inflate
:
setContentView
最后,我只需像往常一样扩展@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//getSupportActionBar().setDisplayShowHomeEnabled(false);
//getSupportActionBar().setDisplayShowCustomEnabled(true);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
//mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
//mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
类和代码。
更新
根据答案,这是我的工作覆盖BaseActivity
:
setContentView
答案 0 :(得分:7)
像这样改变你的setContentView()
......
@Override
public void setContentView(int layoutResID) {
mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
actContent = (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
// set the drawer layout as main content view of Activity.
setContentView(mDrawerLayout);
// add layout of BaseActivities inside framelayout.i.e. frame_container
getLayoutInflater().inflate(layoutResID, actContent, true);
}
答案 1 :(得分:3)
我知道这已经得到了回答,但万一有人在解决我的具体问题:
DrawerLayout
只能有2个孩子,content
和navigationView
。
如果要包含工具栏,则应将其包含在另一个布局内或drawerlayout
:
DrawerLayout
--LinearLayout (or any other layout)
----Toolbar
----Content
--NavigationView
或:
LinearLayout (or any other layout)
--Toolbar
--DrawerLayout
----Content
----NavigationView