【问题标题】:How to have different actions for Navigation Drawer icon and app icon in Action bar?如何在操作栏中对导航抽屉图标和应用程序图标进行不同的操作?
【发布时间】:2014-09-14 06:35:47
【问题描述】:

我在操作栏上有导航抽屉图标和应用程序图标。我需要对两者有不同的操作(导航抽屉图标必须打开导航抽屉,应用程序图标将启动应用程序的主屏幕)。现在它看起来像下面的图片,并且对应用程序图标和导航抽屉图标都有相同的操作。

非常感谢您的帮助!

【问题讨论】:

    标签: android android-actionbar navigation-drawer


    【解决方案1】:

    您所说的 NavigationDrawer 图标实际上并不是一个图标,它是显示导航抽屉是否打开/关闭的附加指示器(放置在 ActionBarDrawerToggle 中),因此单击该区域时只有一个 clickListener 被调用.

    即使可能,用户也会感到非常困惑,因为图标很小且彼此靠近。

    考虑在这种情况下重新设计您的导航流程(顺便说一下,后退按钮应该让您从该活动返回主屏幕)

    【讨论】:

      【解决方案2】:

      不知道好不好,终于找到解决办法了!

      可以使用操作栏的自定义布局来完成。示例代码在这里。

      //mActionbarView is the custom view to be used for action bar
          View mActionBarView = getLayoutInflater().inflate(
                  R.layout.custom_action_bar, null);
      
          Button navDrawer = (Button) mActionBarView.findViewById(R.id.navDrawer);
          Button appIcon = (Button) mActionBarView.findViewById(R.id.appIcon);
      
          navDrawer.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  Toast.makeText(BaseActivty.this, "Drawer", Toast.LENGTH_LONG)
                          .show();
      
                  mDrawerLayout.openDrawer(Gravity.LEFT);
              }
          });
      
          appIcon.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  Toast.makeText(BaseActivty.this, "Home", Toast.LENGTH_LONG)
                          .show();
              }
          });
      
          getActionBar().setCustomView(mActionBarView);
          getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
      

      custom_action_bar.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
          android:id="@+id/navDrawer"
          android:layout_width="30dp"
          android:layout_height="30dp"
          android:background="@drawable/ic_drawer"
          android:padding="5dp" />
      
        <Button
          android:id="@+id/appIcon"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:background="@drawable/ic_launcher" />
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多