当前位置: 首页 > 知识库问答 >
问题:

添加工具栏后,Lollipop上的导航抽屉为空

璩俊雅
2023-03-14

不久前,我有一个导航抽屉在工作,但后来我决定将其更新为使用工具栏,而不是默认的支持操作栏。现在,当我点击汉堡包菜单时,导航抽屉没有打开。然而,我可以从左向右滑动并打开它。问题是,它现在是空的。

值-v21/样式。xml

    <style name="Material" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/app_green</item>
    <item name="colorPrimaryDark">@color/app_green_dark</item>
    <item name="android:textColorPrimary">@color/action_bar_text</item>
    <item name="android:textColor">@color/secondary_text_color</item>
    <item name="android:color">@color/secondary_text_color</item>
    <item name="android:colorAccent">@color/app_green</item>
    <item name="android:editTextColor">@color/secondary_text_color</item>

    <item name="textHeaderMaxLines">@integer/text_header_max_lines</item>
    <item name="trackAbstractMaxLines">@integer/track_abstract_max_lines</item>
    <item name="activatableItemBackground">@drawable/activatable_item_background</item>

    <!-- ActionBar Styles -->
    <item name="android:windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="android:windowActionBar">false</item>

    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">?android:attr/colorPrimaryDark</item>

    <!-- Global UI Assignments -->
    <item name="android:spinnerStyle">@style/Material.Widget.Spinner</item>

    <item name="android:buttonStyle">@style/Material.Widget.Button</item>
    <item name="android:checkboxStyle">@style/Material.Widget.Checkbox</item>
    <item name="android:textAppearance">@android:style/TextAppearance</item>

    <item name="android:popupWindowStyle">@style/Material.Window.Popup</item>

    <!-- ViewPager -->
    <item name="vpiCirclePageIndicatorStyle">@style/Material.Activity.Login.ViewPagerIndicator.CustomCircle</item>
    <item name="buttonBarStyle">?android:buttonBarStyle</item>
    <item name="buttonBarButtonStyle">?android:buttonBarButtonStyle</item>
    <item name="indeterminateProgressStyle">?android:indeterminateProgressStyle</item>
</style>

导航U抽屉。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"
android:fitsSystemWindows="true">

<!-- The main content view. Must be first child because XML ordering implies stacking
 context -->
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <!-- Our App Bar. Placed here so we can draw over it with the navigation drawer. -->
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        android:background="?android:attr/colorPrimary" />

    <!-- Our actual content view. -->
    <FrameLayout
        android:id="@+id/drawer_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

</LinearLayout>

<!-- The navigation drawer itself. -->
<ListView
    android:id="@+id/drawer_list"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:fitsSystemWindows="true"
/>

ctivity.java

    public class NavigationDrawerActivity extends ActionBarActivity
  implements AdapterView.OnItemClickListener {

  private DrawerLayout mDrawerLayout;
  private ListView mDrawerList;
  private LayoutInflater mInflater;
  private NavDrawerItemAdapter mAdapter;
  private ActionBarDrawerToggle mDrawerToggle;

  // Our "App bar". This will only be populated if the current SDK is >= 21. Otherwise, we'll use an
  // action bar that will be populated when the app first starts.
  private Toolbar mAppBar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_drawer);
    mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    setupNavigationDrawer();
  }

  @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) {
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }

  /**
   * Toggles the state of the navigation drawer (i.e. closes it if it's open, and opens it if
   * it's closed).
   */
  public void toggleNavigationDrawer() {
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
      closeNavigationDrawer();
    } else {
      openNavigationDrawer();
    }
  }

  /**
   * Opens the navigation drawer.
   */
  public void openNavigationDrawer() {
    mDrawerLayout.openDrawer(GravityCompat.START);
  }

  /**
   * Closes the navigation drawer.
   */
  public void closeNavigationDrawer() {
    mDrawerLayout.closeDrawer(GravityCompat.START);
  }

  /**
   * Initializes items specific to the navigation drawer.
   */
  private void setupNavigationDrawer() {
    mDrawerLayout = (DrawerLayout) mInflater.inflate(R.layout.navigation_drawer, null); // "null" is important.
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.app_green));

    // We want to use the toolbar if the version is 21 or greater, because is has more flexibility
    // than the standard ActionBar
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      mAppBar = (Toolbar) findViewById(R.id.app_bar);
      setSupportActionBar(mAppBar);
    }

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

    mDrawerToggle = new ActionBarDrawerToggle(
      this,                  /* Our context (Activity that hosts this drawer) */
      mDrawerLayout,         /* The DrawerLayout where the nav drawer will be drawn */
      mAppBar,               /* The Toolbar that is being used as an app bar. */
      R.string.drawer_open,  /* Description of "open drawer", for accessibility */
      R.string.drawer_close  /* Description of "close drawer", for accessibility */
    ) {

      /**
       * Called when a drawer has settled in a completely closed state.
       */
      public void onDrawerClosed(View view) {
        super.onDrawerClosed(view);
        supportInvalidateOptionsMenu();
      }

      /**
       * Called when a drawer has settled in a completely open state.
       */
      public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        supportInvalidateOptionsMenu();
      }
    };

    mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);

    NavDrawerGroup userToolsGroup = new NavDrawerGroup(R.string.drawer_group_usertools, false, 1);
    NavDrawerGroup toolBoxGroup = new NavDrawerGroup(R.string.drawer_group_toolbox, true, 2);
    NavDrawerGroup supportGroup = new NavDrawerGroup(R.string.drawer_group_support, true, 3);

    NavDrawerItem[] navDrawerItems = {
      new NavDrawerItem(R.string.drawer_item_main, R.drawable.ic_main, NavDrawerGroup.DEFAULT_GROUP),
      new NavDrawerItem(R.string.drawer_item_tool_one, R.drawable.ic_tool_one, NavDrawerGroup.DEFAULT_GROUP),
      new NavDrawerItem(R.string.drawer_item_tool_two, R.drawable.ic_tool_two, userToolsGroup),
    };

    mAdapter = new NavDrawerItemAdapter(this, navDrawerItems);
    mDrawerList.setAdapter(mAdapter);

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(this);
  }
}

我知道NavDrawerItem看起来有点不稳定,但我已经验证了它可以与旧的导航抽屉(前工具栏)一起工作,它真正做的是用图标填充列表视图项目,并将它们放置在适当的分组中。因此,我相当有信心,这不是问题所在。

即使是这样,我也应该得到除空白白色背景之外的其他内容,因为导航抽屉的布局实际上为具有绿色背景的列表视图设置了一个标题,我没有看到这个标题。

编辑:

根据@Black sh33p下面的评论,我删除了以不同方式处理不同API级别的代码,并将mDrawerTougle构造html" target="_blank">函数更改为:

嗯,不高兴。我删除了条件检测API级别,只是初始化了Toolbar,然后将mDrawerTougle更改为以下内容:

mDrawerToggle = new ActionBarDrawerToggle(
  this,                  /* Our context (Activity that hosts this drawer) */
  mDrawerLayout,         /* The DrawerLayout where the nav drawer will be drawn */
  mAppBar,               /* The Toolbar that is being used as an app bar. */
  R.string.drawer_open,  /* Description of "open drawer", for accessibility */
  R.string.drawer_close  /* Description of "close drawer", for accessibility */
) {

  /**
   * Called when a drawer has settled in a completely closed state.
   */
  public void onDrawerClosed(View view) {
    super.onDrawerClosed(view);
    supportInvalidateOptionsMenu();
  }

  /**
   * Called when a drawer has settled in a completely open state.
   */
  public void onDrawerOpened(View drawerView) {
    super.onDrawerOpened(drawerView);
    supportInvalidateOptionsMenu();
  }
};

但是,我最终得到了同样的结果:(

共有3个答案

赵星华
2023-03-14

我正在开发一款应用程序,它使用DroperLayout和我自己的工具栏,在我的设备(android 5.0.1)上运行得非常好。为此,我有一个这样的xml布局:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#F78B1E"
        android:minHeight="?attr/actionBarSize" />


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <!-- The navigation drawer -->
        <ListView
            android:id="@+id/left_navigation"
            android:layout_width="400dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#fff"
        />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>
单于经纬
2023-03-14

由于您正在扩展ActionBarActivity,因此不需要以不同的方式处理API级别,您可以使用支持工具栏,一直到API 7。

我认为如果你使用另一个ActionBarDrawerToggle构造函数,你的代码就会工作。试试这个:

mDrawerToggle = new ActionBarDrawerToggle(
      this,                  /* Our context (Activity that hosts this drawer) */
      mDrawerLayout,         /* The DrawerLayout where the nav drawer will be drawn */
      R.string.drawer_open,  /* Description of "open drawer", for accessibility */
      R.string.drawer_close  /* Description of "close drawer", for accessibility */
    ) {

      /**
       * Called when a drawer has settled in a completely closed state.
       */
      public void onDrawerClosed(View view) {
        super.onDrawerClosed(view);
        supportInvalidateOptionsMenu();
      }

      /**
       * Called when a drawer has settled in a completely open state.
       */
      public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        supportInvalidateOptionsMenu();
      }
  };
雷国兴
2023-03-14

从onCreate中删除代码,并将其添加到setContentView中

请参阅此文件,它可能会有所帮助。。!!

 public class DrawerActivity extends ActionBarActivity {

protected ActionBarDrawerToggle mDrawerToggle;
private String[] mModulesTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private Intent intent;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@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 onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.options, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {

        return true;
    }
    switch (item.getItemId()) {
    case R.id.action_search:
        intent = new Intent(this, JoinLeagueActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
        return true;

    case R.id.action_add:
        intent = new Intent(this, CreateLeagueActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
        return true;

    case R.id.contactUs:
        FragmentManager fm = this.getSupportFragmentManager();
        DialogFragment dialog = new ContactUsDialogFragment(); // creating new object
        dialog.show(fm, "dialog");
        return new ContactUsDialogFragment() != null;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void setContentView(final int layoutResID) {

    View fullLayout = (DrawerLayout) getLayoutInflater().inflate(
            R.layout.layout_drawer, null);

    FrameLayout actContent = (FrameLayout) fullLayout
            .findViewById(R.id.content_frame);

    mModulesTitles = getResources().getStringArray(R.array.modules_array);
    mDrawerLayout = (DrawerLayout) fullLayout
            .findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) fullLayout.findViewById(R.id.left_drawer);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);


    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[3], navMenuIcons.getResourceId(3, -1)));
    // Pages
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[4],-1));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[5], -1));
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[6], -1));

    // Recycle the typed array
    navMenuIcons.recycle();
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);
//      mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mModulesTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    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);
        }
    };
    Log.e("DRAWERLAYOUT@@@", "" + mDrawerLayout);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    getLayoutInflater().inflate(layoutResID, actContent, true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    super.setContentView(fullLayout);
}

public class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    public DrawerItemClickListener() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}
private void selectItem(int position) {
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mModulesTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
    if (position == 0) {
        Intent intent = new Intent(this, DashboardActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 1) {
        Intent intent = new Intent(this, LeagueActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 2) {
        Intent intent = new Intent(this, MatchupActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 3) {
        Intent intent = new Intent(this, HelpActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 4) {
        Intent intent = new Intent(this, PrivacyActivity.class);
        intent.putExtra("from_register", false);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 5) {
        Intent intent = new Intent(this, TermsActivity.class);
        intent.putExtra("from_register", false);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 6) {
        SharedPreferenceUtil.putValue("loginStatus", false);
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        this.finish();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
}
}
 类似资料:
  • 请帮帮我,我已经绕了几个小时了!我设置了一个抽屉菜单和一个工具栏(见下面的代码),我无法让返回/回家功能工作,因为单击它会导致打开抽屉。 这是我的主要活动,在OnCreate期间调用。 以下是我片段的OnCreateView方法中的内容。。。 抽屉菜单工作正常。更改工具栏的标题工作正常。汉堡包图标在片段中更改为向后箭头就好了...然而,每次我按下向后箭头,它都会打开抽屉...有点像抽屉的听众在向后

  • 请给我解释一下。。。我的,它与同步(比如)。活动只有很少的片段,在不同的片段中,我需要使用不同的模式(一种模式是视差,另一种模式是简单)。因此,我认为我应该在每个框架中用AppBar和内容设置协调布局 但我如何替换“新建”上的最后一个工具栏以保存与抽屉的同步?或者这是错误的方式,我需要做一些其他的?

  • 问题内容: 在以下活动中,我上面有一个片段和一个图像。片段只是一个较暗的操作栏,上面有图片。我正在尝试将左侧幻灯片菜单作为一个片段,以便可以在每次活动中使用它。 主要活动; 主要活动XML; 每个活动上应包含的标题栏片段; 标题栏片段XML; 如何在标题栏中实现导航抽屉? 问题答案: 尝试这样的操作,对于布局文件,您只需要 实现可以像这样简单。

  • 问题内容: 我在使用Navigation Drawer时遇到问题,它太慢了,我要寻找的解决方案是先关闭抽屉,然后显示活动,但它不起作用,肯定是我遗漏了一些东西。 问题答案: 您可以通过这种方式来避免抽屉滞后,更改 onItemClick : 编辑: 首选方式应该是在DrawerLayout上设置DrawerListener,然后 像这样设置片段:

  • 大家好, 我是一名Android编程新手,目前正在开发一款实践应用程序,它或多或少是教程代码的拼凑。 现在我在工具栏下面有一个导航抽屉。我想重新调整xml层次结构,使其与材料设计指南保持一致,并在工具栏上方具有导航抽屉。这似乎是一项足够简单的任务,但就我而言,我似乎无法完成它。 有人能提供一些建议吗? 上面的代码是这里找到的一个稍微修改的版本,由Ratan编写:https://androidbel

  • 我是android的初学者,我正在尝试不同的东西。我想创建一个与查看页导航抽屉。但每当我这样做时,导航抽屉似乎并没有掩盖顶部的工具栏。我试过很多不同的东西。这是我使用的教程的链接。 救命啊!