【问题标题】:How to Display Navigation Drawer in all activities?如何在所有活动中显示导航抽屉?
【发布时间】:2014-01-28 12:48:23
【问题描述】:

我有一个Navigation Drawer,它应该出现在我的所有活动中。

我看到了很多类似的问题,并找到了一个解决方案,比如用其他活动扩展 MainActivity。

所以我将我的主要活动扩展到我的第二个活动。但是抽屉没有显示在第二个活动中

MainActivity

public class MainActivity extends ActionBarActivity
{
    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
    // getSupportActionBar().hide();
    setContentView(R.layout.activity_main_drawer);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    _initMenu();
    mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
    mDrawer.setDrawerListener(mDrawerToggle);

}

private void _initMenu()
{
    NsMenuAdapter mAdapter = new NsMenuAdapter(this);

    // Add Header
    mAdapter.addHeader(R.string.ns_menu_main_header);

    // Add first block

    menuItems = getResources().getStringArray(R.array.ns_menu_items);
    String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);

    int res = 0;
    for (String item : menuItems)
    {

        int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
        int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());

        NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
        // if (res==1) mItem.counter=12; //it is just an example...
        // if (res==3) mItem.counter=3; //it is just an example...
        mAdapter.addItem(mItem);
        res++;
    }

    mAdapter.addHeader(R.string.ns_menu_main_header2);

    mDrawerList = (ListView) findViewById(R.id.drawer);
    if (mDrawerList != null)
        mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.control_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    // If the nav drawer is open, hide action items related to the content
    // view
    boolean drawerOpen = mDrawer.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_keyboard).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    /*
     * The action bar home/up should open or close the drawer.
     * ActionBarDrawerToggle will take care of this.
     */
    if (mDrawerToggle.onOptionsItemSelected(item))
    {
        return true;
    }

    // Handle your other action bar items...
    return super.onOptionsItemSelected(item);
}

private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
{

    public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
    {
        super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
    }

    @Override
    public void onDrawerClosed(View view)
    {
        getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
        supportInvalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
    }

    @Override
    public void onDrawerOpened(View drawerView)
    {
        getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
        supportInvalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
    }
}

private class DrawerItemClickListener implements ListView.OnItemClickListener
{

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Intent intent = new Intent(MainActivity.this, Tutorial.class);
        startActivity(intent);

    }

}

 }

第二活动

 public class Tutorial extends MainActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
    }

}

【问题讨论】:

  • 一周前我也试过了..没有片段会使代码效率很低..所以最好使用片段。

标签: java android navigation-drawer android-actionbar-compat


【解决方案1】:

这是我的实现..希望对您有所帮助

FIRST,这个POST 是概念。

SECOND,这也是KEY之一。

终于,这里是所有答案的组合


基础活动

这是所有其他活动的基础活动

您可以根据需要扩展 Activity 或 FragmentActivity 等。

Navigation Drawer在这里设置一次。

public class BaseActivity extends FragmentActivity {

    protected DrawerLayout mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.base_layout);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        //This is about creating custom listview for navigate drawer
        //Implementation for NavigateDrawer HERE !
        ArrayList<DrawerListItem> drawerListItems = new ArrayList<DrawerListItem>();
        drawerListItems.add(new DrawerListItem(0,"AIR° DEVICES"));
        drawerListItems.add(new DrawerListItem(1,"A/C Device [1]"));
        drawerListItems.add(new DrawerListItem(1,"A/C Device [2]"));
        drawerListItems.add(new DrawerListItem(1,"A/C Device [3]"));
        drawerListItems.add(new DrawerListItem(0,"AIR° FEATURES"));
        drawerListItems.add(new DrawerListItem(2,"SLEEP MODE"));
        drawerListItems.add(new DrawerListItem(2,"TRACKING MODE"));
        drawerListItems.add(new DrawerListItem(2,"SETTINGS"));
        DrawerAdapter mDrawerAdapter = new DrawerAdapter(this, R.layout.drawer_list_header, drawerListItems);
        ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerList.setAdapter(mDrawerAdapter);
    }

}

基础活动 XML

此 xml 布局适用于 Navigation Drawer

<?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_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    </FrameLayout>

    <!-- The navigation drawer -->
    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:scrollingCache="false"
            android:background="@drawable/drawer_bg"
            android:divider="@null"
            android:choiceMode="singleChoice"/>

</android.support.v4.widget.DrawerLayout>

所有其他活动

其他Activity 只是扩展BaseActivity 并定义代码如下。

Navigation Drawer 将针对特定活动显示。

mDrawerBaseActivity 的形式。这是一个protected 变量。

public class Screen1 extends BaseActivity

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //inflate your activity layout here!
        View contentView = inflater.inflate(R.layout.screen1, null, false);
        mDrawer.addView(contentView, 0);

        //Do the rest as you want for each activity
    }

屏幕 1 XML 示例

按照您的意愿设计每个活动。没有更多的Navigation Drawer 布局!

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

</LinearLayout>

注意

在此实现中,Navigation Drawer 不与操作栏绑定。如果您希望这样做,请在BaseActivity 中进行。此外,本指南并未涵盖所有要求。这只是一个示例。

【讨论】:

  • mDrawer.addView(contentView, 0);给出错误...我需要更改什么才能使其正常工作?
  • 我按照这些说明进行操作,但是当我按下任何抽屉按钮时,我的应用程序会立即崩溃。
  • 是的,没有绑定ActionBar。这是一个真正的问题。我浪费了很多时间,但没有调用onOptionsItemSelected,所以后退箭头不起作用。
  • 这确实有效。现在不再使用片段来使用导航抽屉。
  • 谢谢,我的朋友
【解决方案2】:

TutorialActivityonCreate 中不要调用setContentView,而是这样做:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.help, null, false);
    mDrawer.addView(contentView, 0); 
}

使mDrawer 中的MainActivity 受保护。在R.layout.activity_main_drawer 中只保留抽屉标签和重力左(或右)元素。

【讨论】:

  • 这真的很有帮助,花了一整天。使用它并在几秒钟内完成工作。谢谢你
  • 请添加R.layout.activity_main_drawer xml文件回答
  • 什么?为什么不用&lt;include&gt;?!
  • @СергейГрушин 我认为是因为 DrawerLayout 应该是顶部元素。无论如何,我建议一个更好的解决方案是现在使用片段。如果有办法让用户 分享如何。
  • 我删除 setContentView() 并写下你所说的,但对我不起作用......我的 baseActivity 在 SecondActivity 上打开。
【解决方案3】:

我做了一个 BaseActivity 活动,它扩展了 SherlockActivity(或 ActionBarActivity,如果是你的情况)

public class BaseActivity extends SherlockActivity

然后,让您的所有活动都扩展 BaseActivity,例如:

public class GlossaryActivity extends BaseActivity

稍后,您必须将活动布局替换为与您的活动对应的布局,我在 BaseActivity 中做了一个这样的方法:

protected void replaceContentLayout(int sourceId, int destinationId) {
    View contentLayout = findViewById(destinationId);

    ViewGroup parent = (ViewGroup) contentLayout.getParent();
    int index = parent.indexOfChild(contentLayout);

    parent.removeView(contentLayout);
    contentLayout = getLayoutInflater().inflate(sourceId, parent, false);
    parent.addView(contentLayout, index);
}

我在每个activity的onCreate方法上调用了这个方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.replaceContentLayout(R.layout.activity_glossary, super.CONTENT_LAYOUT_ID);

}

super.CONTENT_LAYOUT_ID是BaseActivity的FrameLayout,其他参数是你要替换的布局

【讨论】:

    【解决方案4】:

    您从派生类onCreate 中省略了@Override

    更新:我不确定两次调用setContentView 会产生什么影响,但这可能是问题所在。分离出设置抽屉的代码,并从两个onCreate 方法中调用它。

    【讨论】:

      【解决方案5】:

      我也有这个问题。这是我的实现:

      activity_main.xml - CoordinatorLayout 中索引 1 处的子项是 content_main.xml,我可以在代码中更改。

      <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:openDrawer="start">
      
          <android.support.design.widget.CoordinatorLayout
              android:id="@+id/coordinator"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fitsSystemWindows="true">
      
              <android.support.design.widget.AppBarLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:theme="@style/AppTheme.AppBarOverlay">
      
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:background="?attr/colorPrimary"
                      app:popupTheme="@style/AppTheme.PopupOverlay" />
      
              </android.support.design.widget.AppBarLayout>
      
              **<include layout="@layout/content_main" />**
      
              <android.support.design.widget.FloatingActionButton
                  android:id="@+id/fab"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="bottom|end"
                  android:layout_margin="@dimen/fab_margin"
                  android:src="@android:drawable/ic_dialog_email" />
      
          </android.support.design.widget.CoordinatorLayout>
      
          <android.support.design.widget.NavigationView
              android:id="@+id/nav_view"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_gravity="start"
              android:fitsSystemWindows="true"
              app:headerLayout="@layout/nav_header_main"
              app:menu="@menu/activity_main_drawer" />
      
      </android.support.v4.widget.DrawerLayout>
      

      我创建了一个使用膨胀其他活动 UI 的类:

      public class MyLayoutInflater {
      
          public void inflate(Activity activity, int LayoutResource, android.app.ActionBar getSupportActionBar, Intent getIntent){
              CoordinatorLayout coordinatorLayout = (CoordinatorLayout) activity.findViewById(R.id.coordinator);
              android.view.LayoutInflater inflater = (android.view.LayoutInflater) activity
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View contentView = inflater.inflate(LayoutResource, null, false);
      
              //change i so that it suits the child number in you coordinator layout
              int i = 1;
              coordinatorLayout.removeViewAt(i);
              coordinatorLayout.addView(contentView, i);
              getSupportActionBar.setTitle(actionBarTitle);
          }
      
          public void inflate(Activity activity, int LayoutResource, android.support.v7.app.ActionBar getActionBar, String actionBarTitle){
              CoordinatorLayout coordinatorLayout = (CoordinatorLayout) activity.findViewById(R.id.coordinator);
              android.view.LayoutInflater inflater = (android.view.LayoutInflater) activity
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View contentView = inflater.inflate(LayoutResource, null, false);
      
              //change i so that it suits the child number in you coordinator layout
              int i = 1;
              coordinatorLayout.removeViewAt(i);
              coordinatorLayout.addView(contentView, i);
              getActionBar.setTitle(actionBarTitle);
          }
      
      }
      

      现在在其他活动上,您所要做的就是扩展 MainActivity 并调用此方法并为其提供必要的参数:

      public class AnotherActivity extends MainActivity {
      
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              new MyLayoutInflater().inflate(this,R.layout.content_activity_another, getSupportActionBar(), getIntent());
      
          }
      }
      

      【讨论】:

      • 在您的第一个 inflate 方法中,您不使用 getIntent 参数。
      【解决方案6】:

      好的,这是一种很老套的方法,我只将它用于特殊类型的调试构建,以实时设置视图的属性(设计工具)。

      它的优点是您可以像往常一样使用您的孩子的活动,而不会出现不同答案中需要的特殊行为。

      所以在 BaseActvity 中你可以添加:

      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      
      // WARNING: Hacky, use carefully!!!
      ViewGroup androidBaseView = (ViewGroup) findViewById(android.R.id.content);
      //this one in what child activity has just set in setContentView()
      ViewGroup childContent = (ViewGroup) androidBaseView.getChildAt(0);
      
      View drawerView = LayoutInflater.from(this)
          .inflate(R.layout.base_activity_drawer, androidBaseView, false);
      FrameLayout frameLayout = (FrameLayout) drawerView.findViewById(R.id.content);
      androidBaseView.removeView(childContent);
      frameLayout.addView(childContent);
      androidBaseView.addView(drawerView);
      }
      

      而抽屉的 xml 只是:

        <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <FrameLayout
          android:id="@+id/content"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
        <LinearLayout
          android:id="@+id/drawer_for_components"
          android:layout_width="240dp"
          android:layout_height="match_parent"
          android:layout_gravity="end"
          android:orientation="vertical"
          android:fitsSystemWindows="true"
          />
      </android.support.v4.widget.DrawerLayout>
      

      【讨论】:

        【解决方案7】:

        这是在 android studio 中简单快速的方法:

        1. 从图库中创建一个新的活动(导航抽屉活动),并将其命名为您想要的任何名称,android studio 将为您创建所有内容(您可以稍后自定义的类和 xml 文件)

        2. 在其他活动中,您应该扩展导航抽屉活动,并确保这些其他活动在清单文件中具有“无操作栏”(android:theme="@style/AppTheme.NoActionBar")

        3. 您应该如下修改您的其他活动:

          public class Mainactivity extends NavActivity
          {
          super.onCreate(savedInstanceState);
             LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             //inflate your activity layout here!
             View contentView = inflater.inflate(R.layout.activity_main, null, false);
             drawer.addView(contentView, 0);
          }
          

        注意:mainactivity会扩展NavActivity的action bar,NavActivity有一个全功能的action bar会调用导航抽屉

        希望对你有用

        【讨论】:

          【解决方案8】:

          现在您应该使用单活动应用架构 (source)。

          然后简单地将导航抽屉添加到主活动

          【讨论】:

            【解决方案9】:

            你可以简单地使用&lt;include/&gt; 通过创建导航抽屉

             <?xml version="1.0" encoding="utf-8"?>
            <androidx.drawerlayout.widget.DrawerLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/drawer_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                tools:openDrawer="start">
            

            然后在其中包含布局

                <include
                    layout="@layout/activity_accounts"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            

            在你的主要活动中制作setContentView(R.layout.your_drawer_activity)

            请注意,如果您使用此方法,则必须为您拥有的每个活动创建一个导航抽屉布局,除非您找到了一种以编程方式执行包含的方法。

            【讨论】:

              猜你喜欢
              • 2020-04-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多