Introduction to DrawerLayout drawer effect and drawerlayout drawer
DrawerLayout is a left slide right slide drawer layout provided under the V4 package.
The implementation result is as follows:
Because it is officially provided, it is relatively simple to use.
Provided by DrawerLayout
1. When the page pops up, the background of the main content area turns black automatically. When you click the content area, the drawer layout disappears.
2. sliding at the edge of the screen will pull out the drawer Layout
Note: When you press the back key, if the drawer layout is displayed, you need to close the drawer layout.
The following is a simple Demo:
Bytes --------------------------------------------------------------------------------------------
First, layout the file:
The layout is divided into three parts: Left, middle, and right. In the middle is the main content area displayed by the APP. Then, you can choose whether to display the layout on the left or on the right based on your personal needs.
If you select the Left or Right pop-up interface, you only need to set the pop-up Interface
Android: layout_gravity = "left" or android: layout_gravity = "right ".
1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical" 6 tools: context = ". drawerActivity "> 7 8 9 <android. support. v4.widget. drawerLayout10 android: id = "@ + id/drawer_layout" 11 android: layout_width = "match_parent" 12 android: lay Out_height = "match_parent" 13> 14 15 <! -- The main content view --> 16 <! -- Main content layout area --> 17 <FrameLayout18 android: id = "@ + id/content_frame" 19 android: layout_width = "match_parent" 20 android: layout_height = "match_parent"> 21 22 <Button23 android: id = "@ + id/btn" 24 android: layout_width = "match_parent" 25 android: layout_height = "wrap_content" 26 android: text = "open" 27/> 28 </FrameLayout> 29 30 <! -- The navigation drawer -->
<! -- Pop-up layout area on the left -->
31 <LinearLayout32 android: layout_width = "100dp" 33 android: layout_height = "match_parent" 34 android: orientation = "vertical" 35 android: layout_gravity = "left" 36 android: background = "# fff" 37> 38 <TextView39 android: layout_width = "wrap_content" 40 android: layout_height = "wrap_content" 41 android: text = "DrawerLayout drawer effect" 42/> 43 <TextView44 android: layout_width = "wrap_content" 45 android: layout_height = "wrap_content" 46 android: text = "text" 47/> 48 <Button49 android: layout_width = "wrap_content" 50 android: layout_height = "wrap_content" 51 android: text = "button"/> 52 </LinearLayout> 53 54 </android. support. v4.widget. drawerLayout> 55 </LinearLayout>
Then read the activity code:
As long as mDrawerLayout. openDrawer (Gravity. LEFT );
Pay attention to rewrite the back button to respond to events. When the drawer layout is displayed, close the drawer layout first.
Public class MainActivity extends Activity {private DrawerLayout mDrawerLayout = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mDrawerLayout = (DrawerLayout) findViewById (R. id. drawer_layout); mDrawerLayout. setDrawerLockMode (DrawerLayout. LOCK_MODE_UNLOCKED); Button button = (Button) findViewById (R. id. Btn); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// click the button to open mDrawerLayout. openDrawer (Gravity. LEFT) ;}}); mDrawerLayout. setDrawerListener (new DrawerLayout. drawerListener () {/*** called when the drawer slide status changes * The status value is STATE_IDLE (idle -- 0), STATE_DRAGGING (dragging -- 1 ), STATE_SETTLING (fixed -- 2. * When the drawer is opened, click the drawer. The drawer status changes to STATE_DRAGGING and then to STATE_IDLE */@ Override public void onDrawerStateChanged (int arg0) {Log. I ("drawer", "drawer status:" + arg0 );} /*** call this method when the drawer is sliding * arg1 indicates the sliding amplitude (0-1) */@ Override public void onDrawerSlide (View arg0, float arg1) {Log. I ("drawer", arg1 + "");}/*** called when a drawer is fully opened */@ Override public void onDrawerOpened (View arg0) {Log. I ("drawer", "the drawer is completely opened! ");}/*** Call this method when a drawer is completely closed */@ Override public void onDrawerClosed (View arg0) {Log. I ("drawer", "the drawer is completely closed! ") ;}}) ;}@ Override public void onBackPressed () {super. onBackPressed (); if (mDrawerLayout! = Null) {if (mDrawerLayout. isDrawerOpen (Gravity. LEFT) {mDrawerLayout. closeDrawers () ;}else super. onBackPressed ();}}}
For more information, see the monitoring events of DrawerLayout:
MDrawerLayout. setDrawerListener (new DrawerLayout. drawerListener () {/*** called when the drawer slide status changes * The status value is STATE_IDLE (idle -- 0), STATE_DRAGGING (dragging -- 1 ), STATE_SETTLING (fixed -- 2. * When the drawer is opened, click the drawer. The drawer status changes to STATE_DRAGGING and then to STATE_IDLE */@ Override public void onDrawerStateChanged (int arg0) {Log. I ("drawer", "drawer status:" + arg0 );} /*** call this method when the drawer is sliding * arg1 indicates the sliding amplitude (0-1) */@ Override public void onDrawerSlide (View arg0, float arg1) {Log. I ("drawer", arg1 + "");}/*** called when a drawer is fully opened */@ Override public void onDrawerOpened (View arg0) {Log. I ("drawer", "the drawer is completely opened! ");}/*** Call this method when a drawer is completely closed */@ Override public void onDrawerClosed (View arg0) {Log. I ("drawer", "the drawer is completely closed! ");}});