使用自定义操作栏无法看到导航抽屉图标?

时间:2015-06-08 10:35:18

标签: android navigation-drawer

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

Activty

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

NavigationDrawer Fragment

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 */
) {...

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

1 个答案:

答案 0 :(得分: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);
                    }


            }
        }); 
相关问题