Use DrawerLayout to implement simple drawer effects.
Preface: This article is the most basic use of DrawerLayout to achieve the drawer effect. I also try to streamline the code to be the most efficient, and I will post other complicated functions later.
First look
The text in the title bar will display different styles Based on the selected item.
Directly add the code.
The following is the activity_main code.
<RelativeLayout 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"> <android. support. v4.widget. drawerLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/drawer_layout" android: layout_width = "match_parent" android: layout_height = "match_parent"> <! -- Content view displayed on the current main interface --> <! -- The main content must be the first element of DrawerLayout, because it displays the first one, and the drawer must be above the content --> <FrameLayout android: id = "@ + id/content_frame" android: layout_width = "match_parent" android: layout_height = "match_parent"/> <! -- Navigation drawer View --> <ListView android: id = "@ + id/left_drawer" android: layout_width = "240dp" android: layout_height = "match_parent" android: layout_gravity = "left" android: background = "@ android: color/holo_red_dark" android: choiceMode = "singleChoice" android: divider = "@ android: color/transparent" android: dividerHeight = "5dp"/> </android. support. v4.widget. drawerLayout> </RelativeLayout>
- The width of the drawer menu is
dp
The height is the same as that of the parent View. The width of the drawer menu should not exceed 320dp, so that you can see part of the content interface when the menu is opened.
The following is the MainActivity code.
Package com. example. chouti; import android. OS. bundle; import android. app. activity; import android. content. res. configuration; import android. view. menuItem; import android. view. view; import android. view. window; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listView; import android. support. v4.app. actionBarDrawer Toggle; import android. support. v4.view. gravityCompat; import android. support. v4.widget. drawerLayout; public class MainActivity extends Activity {private String [] mPlanetTitles = {"hahhooo", "jdahldfj", "Is this true?", "it should be true", "4 ", "jdahldfj", "Is this true?", "it should be true", "jdahldfj", "Is it true", "it should be true"}; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; private ListView mDrawerList; @ Overri Depublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mDrawerLayout = (DrawerLayout) findViewById (R. id. drawer_layout); // initialize ListView and adapt the data initListView (); // mDrawerLayout. setDrawerShadow (R. drawable. ic_launcher, GravityCompat. START); // initialize ActionBarDrawerTogglemDrawerToggle = new ActionBarDrawerToggle (this, // display the Activity object mDrawerL of the drawer Ayout, // DrawerLayout object R. drawable. ic_launcher, // A drawable resource used to indicate the drawer (if you want to display this icon, you must override onPostCreate (Bundle savedInstanceState) R. string. hello_world, // a text used to describe opening a drawer (used to support accessibility) R. string. hello_world); // a text used to describe the close drawer (used to support accessibility) // set ActionBarDrawerToggle to DrawerListenermDrawerLayout. setDrawerListener (mDrawerToggle); // enable the ActionBar operation mode (call this method and click the icon to respond) getActionBar (). setDisplayHomeAsUpEnabled (true); getActio NBar (). setHomeButtonEnabled (true); // Note: The getActionBar () is added at API level 11} // onPostCreate (Bundle savedInstanceState) method // @ Override // protected void onPostCreate (Bundle savedInstanceState) {/// super. onPostCreate (savedInstanceState); // mDrawerToggle. syncState (); //} private void initListView () {mDrawerList = (ListView) findViewById (R. id. left_drawer); // applicable to the mDrawerList data. setAdapter (new ArrayAdapter <Stri Ng> (this, android. r. layout. simple_list_item_1, mPlanetTitles); // sets the listener event mDrawerList for listView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// update the title, and close the drawer setTitle (mPlanetTitles [position]); mDrawerLayout. closeDrawer (mDrawerList) ;}}) ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// transmits the event to ActionBarDrawerToggle. if true is returned, it processes the icon RESPONSE event if (mDrawerToggle. onOptionsItemSelected (item) {return true;} // process other operations. Click return super. onOptionsItemSelected (item );}}
For more information about ActionBar, click here
Or http://blog.csdn.net/harryweasley/article/details/42027563
I have reproduced the above three articles to help me complete the above project.