Android:多个活动的导航抽屉
Posted
技术标签:
【中文标题】Android:多个活动的导航抽屉【英文标题】:Android: Navigation drawer on multiple activities 【发布时间】:2014-08-22 20:14:56 【问题描述】:我正在尝试通过创建我的所有其他活动都继承自的 DrawerActivity 来实现多个活动的导航抽屉。
我已使用以下 2 个链接将我带到我所在的位置:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
sliding drawer appear in all activities
我可以看到导航抽屉(抓取手柄),但是我无法通过单击操作栏中的标题来打开它,也看不到抽屉中的任何项目。但是,我可以通过滑动打开它。
DrawerActivity.java
public class DrawerActivity extends Activity
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] navOptions = new String[] "Collect Offers",
"Exclusive", "Glove Box", "Servicing", "Dealer", "Settings";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.nav_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(
this, R.layout.drawer_list_item, navOptions));
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
)
public void onDrawerClosed(View view)
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
public void onDrawerOpened(View drawerView)
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
;
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
@Override
public boolean onOptionsItemSelected(MenuItem item)
if (mDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
@Override
public void setContentView(final int layoutResID)
DrawerLayout fullLayout = (DrawerLayout) getLayoutInflater()
.inflate(R.layout.activity_drawer, null);
LinearLayout actContent = (LinearLayout) fullLayout
.findViewById(R.id.content);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
activity_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:orientation="vertical"
android:layout_
android:layout_
android:background="@drawable/infiniti_splash_2x">
</LinearLayout>
<ListView android:id="@+id/nav_drawer"
android:layout_
android:layout_
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
HomeActivity.java
public class HomeActivity extends DrawerActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
@Override
public boolean onOptionsItemSelected(MenuItem item)
return super.onOptionsItemSelected(item);
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_>
</LinearLayout>
【问题讨论】:
【参考方案1】:解决了我的问题。问题在于,通过覆盖 setContentView,它在 ActionBarDrawerToggle 已经设置之后被第二次调用(来自 HomeActivity OnCreate())。这意味着我基本上覆盖了 DrawerActivity OnCreate() 中完成的所有工作。
这是我在 DrawerActivity 中修改的代码
public class DrawerActivity extends Activity
protected ActionBarDrawerToggle mDrawerToggle;
private String[] navOptions = new String[] "Collect Offers", "Exclusive", "Glove Box",
"Servicing", "Dealer", "Settings";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_drawer); NO LONGER REQUIRED
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
@Override
public boolean onOptionsItemSelected(MenuItem item)
if (mDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
@Override
public void setContentView(final int layoutResID)
DrawerLayout fullLayout= (DrawerLayout) getLayoutInflater()
.inflate(R.layout.activity_drawer, null);
LinearLayout actContent= (LinearLayout) fullLayout.findViewById(R.id.content);
DrawerLayout mDrawerLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) fullLayout.findViewById(R.id.nav_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, navOptions));
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
)
public void onDrawerClosed(View view)
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
public void onDrawerOpened(View drawerView)
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
;
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
【讨论】:
以上是关于Android:多个活动的导航抽屉的主要内容,如果未能解决你的问题,请参考以下文章