问题描述
我试图按照这个的答案,主要思想是覆盖的setContentView
在 BaseActivity
,将由所有活动延伸活动所以只有 BaseActivity
将有抽屉式导航。
不过,我的导航从未显示(甚至在 BaseActivity
)后,我试图执行了答案。这是我做的:
这是抽屉式导航栏的XML:
< android.support.v4.widget.DrawerLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<! - 的FrameLayout显示片段 - >
<的FrameLayout
机器人:ID =@ + ID / frame_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
<! - 列表视图显示滑块菜单 - >
<的ListView
机器人:ID =@ + ID / list_slidermenu
机器人:layout_width =120dp
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:背景=@色/ list_background
机器人:choiceMode =singleChoice
机器人:分隔=@色/ list_divider
机器人:dividerHeight =1DP
机器人:listSelector =@可绘制/ list_selector/>
< /android.support.v4.widget.DrawerLayout>
这是我overrided 的setContentView
(在 BaseActivity
):
@覆盖
公共无效的setContentView(INT layoutResID){
// TODO自动生成方法存根
mDrawerLayout =(DrawerLayout)getLayoutInflater()膨胀(R.layout.activity_main,空)。 //你的基地,这里布局
actContent =(的FrameLayout)mDrawerLayout.findViewById(R.id.frame_container);
// listLayout =(的LinearLayout)mDrawerLayout.findViewById(R.id.listLayout);
。getLayoutInflater()膨胀(layoutResID,actContent,真正的);
//getLayoutInflater().inflate(layoutResID,listLayout,真正的);
super.setContentView(layoutResID);
}
这是我的的onCreate
在 BaseActivity
,请注意,我用 getLayoutInflater() .inflate
而不是的setContentView
:
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//getSupportActionBar().setDisplayShowHomeEnabled(false);
//getSupportActionBar().setDisplayShowCustomEnabled(true);
//使操作栏中的应用程序图标,它表现为切换按钮
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
getSupportActionBar()setHomeButtonEnabled(真)。
mTitle = mDrawerTitle =的getTitle();
//负荷滑动菜单项
navMenuTitles = getResources()getStringArray(R.array.nav_drawer_items)。
//导航抽屉,从资源的图标
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
// mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerLayout =(DrawerLayout)getLayoutInflater()膨胀(R.layout.activity_main,空)。
// mDrawerList =(ListView控件)findViewById(R.id.list_slidermenu);
mDrawerList =(ListView控件)mDrawerLayout.findViewById(R.id.list_slidermenu);
navDrawerItems =新的ArrayList< NavDrawerItem>();
//添加导航抽屉里的物品数组
// 家
navDrawerItems.add(新NavDrawerItem(navMenuTitles [0],navMenuIcons.getResourceId(0,-1)));
//查找人员
navDrawerItems.add(新NavDrawerItem(navMenuTitles [1],navMenuIcons.getResourceId(1,-1)));
//照片
navDrawerItems.add(新NavDrawerItem(navMenuTitles [2],navMenuIcons.getResourceId(2,-1)));
//社区,这里将添加计数器
navDrawerItems.add(新NavDrawerItem(navMenuTitles [3],navMenuIcons.getResourceId(3,-1)));
//网页
navDrawerItems.add(新NavDrawerItem(navMenuTitles [4],navMenuIcons.getResourceId(4,-1)));
//热点,我们将在这里添加计数器
navDrawerItems.add(新NavDrawerItem(navMenuTitles [5],navMenuIcons.getResourceId(5,-1)));
//回收类型数组
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(新SlideMenuClickListener());
//设置导航抽屉名单适配器
适配器=新NavDrawerListAdapter(getApplicationContext()
navDrawerItems);
mDrawerList.setAdapter(适配器);
mDrawerToggle =新ActionBarDrawerToggle(这一点,mDrawerLayout,
R.drawable.ic_drawer,//导航菜单切换图标
R.string.app_name,//导航抽屉打开 - 说明访问
R.string.app_name //导航抽屉关闭 - 说明访问
){
公共无效onDrawerClosed(查看视图){
。getSupportActionBar()的setTitle(mTitle);
//调用prepareOptionsMenu()来显示操作栏图标
invalidateOptionsMenu();
}
公共无效onDrawerOpened(查看drawerView){
。getSupportActionBar()的setTitle(mDrawerTitle);
//调用prepareOptionsMenu()隐藏操作栏图标
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(savedInstanceState == NULL){
//关于第一导航项目首次显示视图
displayView(0);
}
}
最后,我只需要延长 BaseActivity
类和code像往常一样。
更新
据答案,这是我的工作overrided 的setContentView
:
@覆盖
公共无效的setContentView(最终诠释layoutResID){
// TODO自动生成方法存根
mDrawerLayout =(DrawerLayout)getLayoutInflater()膨胀(R.layout.activity_main,空)。 //你的基地,这里布局
actContent =(的FrameLayout)mDrawerLayout.findViewById(R.id.frame_container);
。getLayoutInflater()膨胀(layoutResID,actContent,真正的);
super.setContentView(mDrawerLayout);
mTitle = mDrawerTitle =的getTitle();
//负荷滑动菜单项
navMenuTitles = getResources()getStringArray(R.array.nav_drawer_items)。
//导航抽屉,从资源的图标
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerList =(ListView控件)mDrawerLayout.findViewById(R.id.list_slidermenu);
navDrawerItems =新的ArrayList< NavDrawerItem>();
//添加导航抽屉里的物品数组
// 家
navDrawerItems.add(新NavDrawerItem(navMenuTitles [0],navMenuIcons.getResourceId(0,-1)));
//查找人员
navDrawerItems.add(新NavDrawerItem(navMenuTitles [1],navMenuIcons.getResourceId(1,-1)));
//照片
navDrawerItems.add(新NavDrawerItem(navMenuTitles [2],navMenuIcons.getResourceId(2,-1)));
//社区,这里将添加计数器
navDrawerItems.add(新NavDrawerItem(navMenuTitles [3],navMenuIcons.getResourceId(3,-1)));
//网页
navDrawerItems.add(新NavDrawerItem(navMenuTitles [4],navMenuIcons.getResourceId(4,-1)));
//热点,我们将在这里添加计数器
navDrawerItems.add(新NavDrawerItem(navMenuTitles [5],navMenuIcons.getResourceId(5,-1)));
//回收类型数组
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(新SlideMenuClickListener());
//设置导航抽屉名单适配器
适配器=新NavDrawerListAdapter(getApplicationContext()
navDrawerItems);
mDrawerList.setAdapter(适配器);
mDrawerToggle =新ActionBarDrawerToggle(这一点,mDrawerLayout,
R.drawable.ic_drawer,//导航菜单切换图标
R.string.app_name,//导航抽屉打开 - 说明访问
R.string.app_name //导航抽屉关闭 - 说明访问
){
公共无效onDrawerClosed(查看视图){
。getSupportActionBar()的setTitle(mTitle);
//调用prepareOptionsMenu()来显示操作栏图标
invalidateOptionsMenu();
}
公共无效onDrawerOpened(查看drawerView){
。getSupportActionBar()的setTitle(mDrawerTitle);
//调用prepareOptionsMenu()隐藏操作栏图标
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
//super.setContentView(mDrawerLayout);
/*getLayoutInflater().inflate(layoutResID,actContent,真正的);
super.setContentView(mDrawerLayout); * /
}
更改的setContentView()
这样的...
@覆盖
公共无效的setContentView(INT layoutResID){
mDrawerLayout =(DrawerLayout)getLayoutInflater()膨胀(R.layout.activity_main,空)。
actContent =(的FrameLayout)mDrawerLayout.findViewById(R.id.frame_container);
//设置抽屉布局,活动的主要内容视图。
的setContentView(mDrawerLayout);
//里面添加framelayout.ie BaseActivities布局frame_container
。getLayoutInflater()膨胀(layoutResID,actContent,真正的);
}
I tried to follow this answer, with the main idea is to override the setContentView
in the BaseActivity
, the activity that will be extended by all activity so only BaseActivity
will have the navigation drawer.
However, my navigation is never shown (even in BaseActivity
) after i tried to implement the answer. This is what i did :
This is the navigation drawer's xml :
<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>
This is my overrided setContentView
(in 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);
}
And this is my onCreate
on BaseActivity
, note that i use getLayoutInflater().inflate
instead of 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);
}
}
And finally, i just need to extend the BaseActivity
class and code like usual.
UPDATE
According to the answer, this is my WORKING overrided setContentView
:
@Override
public void setContentView(final 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);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(mDrawerLayout);
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);
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);
//super.setContentView(mDrawerLayout);
/*getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(mDrawerLayout);*/
}
Change your setContentView()
like this...
@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);
}
这篇关于创建活动的基础与导航抽屉的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!