ActionBar、Drawerlayout、ActionBarDrawerToggle实现抽屉效果

    xiaoxiao2021-12-14  17

    写好Drawerlayout布局

    实现抽屉布局 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:id="@+id/dl"> <!-- the main layout --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <!-- the left content layout --> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_tab" android:layout_gravity="left"> </FrameLayout> </android.support.v4.widget.DrawerLayout>

    拿到ActionBar和新建ActionBarDrawerToggle对象

    控制抽屉的开关, 显示在actionBar 上面 ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true);//显示抽屉开关 drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer_am, R.string.open_drawer, R.string.close_drawer){ @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); Toast.makeText(getApplicationContext(), "抽屉关闭了", 0).show(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); Toast.makeText(getApplicationContext(), "抽屉打开了", 0).show(); } }; mDrawerLayout.setDrawerListener(drawerToggle); // 让开关和actionbar建立关系 drawerToggle.syncState(); /** 处理actionBar菜单条目的点击事件 */ public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_search) { Toast.makeText(getApplicationContext(), "搜索", 0).show(); } //要使点击actionBar的item后抽屉响应,就要将item参数交给drawerToggle.onOptionsItemSelected()方法处理 return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item); }
    转载请注明原文地址: https://ju.6miu.com/read-971035.html

    最新回复(0)