【问题标题】:Navigation Drawer Icon not visible with custom action bar?自定义操作栏不显示导航抽屉图标?
【发布时间】:2015-06-08 10:35:17
【问题描述】:

我正在尝试将导航抽屉与自定义操作栏一起使用。我的操作栏不包含任何菜单。
我看到抽屉关闭时,导航抽屉图标在操作栏上不可见。即使我在ActionbarToggle 实现中给出了抽屉图标。抽屉图标在navigation drawer 打开时出现,但在navigation drawer 关闭时消失。

活动

 public void setCustomActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.action_bar);
}

NavigationDrawer 片段

getActivity(),                    /* host Activity */
        mDrawerLayout,                    /* DrawerLayout object */
        R.drawable.drawer,             /* nav drawer image to replace 'Up' caret */
        R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
        R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
) {...

如您所见,我在上面提供了抽屉图标。 为什么会发生这种情况,解决方法是什么?

【问题讨论】:

  • 你需要使用getActionBar().setIcon(R.drawable.);设置图标
  • @MeenalSharma :不,它不起作用:(

标签: android navigation-drawer


【解决方案1】:

我假设您正在创建自己的自定义布局。假设我有下面给出的这个 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="#cccccc">
    <Button 
        android:layout_width="10dp"
        android:id="@+id/drawer_toggle"
        android:layout_height="40dp"
        android:background="#000000"
        android:layout_margin="10dp"
        android:layout_gravity="center_vertical|start"/>
    </FrameLayout>
</LinearLayout>

那么你可以这样做:

Button toggle = (Button) yourDrawerView.findViewById(R.id.drawer_toggle);   
toggle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                boolean isDrawerOpen = yourDrawerLayoutRefference.isDrawerOpen(yourDrawerLayout);
                if(isDrawerOpen){
                         toggle.closeDrawer(yourDrawerLayout);
                       }
                else{
                       toggle.openDrawer(yourDrawerLayout);
                    }


            }
        }); 

【讨论】:

  • "然后在右上角提供一个按钮并在其上打开和关闭抽屉。" - 我没有得到这部分。
猜你喜欢
  • 1970-01-01
  • 2013-06-06
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多