Android -- DrawerLayout
抽屜效果的導航菜單??????????????????????????????????????????????????????????????????
喜歡知乎的都應(yīng)該裝的用知乎日報吧~這里指Android的不是IOS的。知乎日報的導航菜單就是用DrawerLayout實現(xiàn)的。
覺得這種側(cè)滑的抽屜效果的菜單很好。
不用切換到另一個頁面,也不用去按菜單的硬件按鈕,直接在界面上一個按鈕點擊,菜單就滑出來,而且感覺能放很多東西。
?
?
?
?
?
android-support-v4.jar?????????????????????????????????????????????????????????????
首先, DrawerLayout這個類是在Support Library里的,需要加上android-support-v4.jar這個包。
然后程序中用時在前面導入import android.support.v4.widget.DrawerLayout;
如果找不到這個類,首先用SDK Manager更新一下Android Support Library,然后在Android SDK\extras\android\support\v4路徑下找到android-support-v4.jar,復制到項目的libs路徑,將其Add to Build Path.
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.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent" ><!-- The main content view --><!-- main content must be the first element of DrawerLayout because it will be drawn first and drawer must be on top of it --><FrameLayoutandroid:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="match_parent" /><!-- The navigation drawer --><ListViewandroid:id="@+id/left_drawer"android:layout_width="240dp"android:layout_height="match_parent"android:layout_gravity="left"android:background="#111"android:choiceMode="singleChoice"android:divider="@android:color/transparent"android:dividerHeight="0dp" /></android.support.v4.widget.DrawerLayout></RelativeLayout>
DrawerLayout的第一個子元素是主要內(nèi)容,即抽屜沒有打開時顯示的布局。這里采用了一個FrameLayout,里面什么也沒放。
DrawerLayout的第二個子元素是抽屜中的內(nèi)容,即抽屜布局,這里采用了一個ListView。
import android.os.Bundle; import android.app.Activity; import android.content.res.Configuration; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout;public class HelloDrawerActivity extends Activity {private String[] mPlanetTitles;private DrawerLayout mDrawerLayout;private ActionBarDrawerToggle mDrawerToggle;private ListView mDrawerList;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_hello_drawer);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);// init the ListView and Adapter, nothing new initListView();// set a custom shadow that overlays the main content when the drawer// opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close){/** Called when a drawer has settled in a completely closed state. */public void onDrawerClosed(View view){invalidateOptionsMenu(); // creates call to// onPrepareOptionsMenu() }/** Called when a drawer has settled in a completely open state. */public void onDrawerOpened(View drawerView){invalidateOptionsMenu(); // creates call to// onPrepareOptionsMenu() }};// Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle);// enable ActionBar app icon to behave as action to toggle nav drawergetActionBar().setDisplayHomeAsUpEnabled(true);// getActionBar().setHomeButtonEnabled(true);// Note: getActionBar() Added in API level 11 }private void initListView(){mDrawerList = (ListView) findViewById(R.id.left_drawer);mPlanetTitles = getResources().getStringArray(R.array.planets_array);// Set the adapter for the list viewmDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, mPlanetTitles));// Set the list's click listenermDrawerList.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id){// Highlight the selected item, update the title, and close the// drawermDrawerList.setItemChecked(position, true);setTitle(mPlanetTitles[position]);mDrawerLayout.closeDrawer(mDrawerList);}});}@Overrideprotected void onPostCreate(Bundle savedInstanceState){super.onPostCreate(savedInstanceState);// Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState();}@Overridepublic void onConfigurationChanged(Configuration newConfig){super.onConfigurationChanged(newConfig);mDrawerToggle.onConfigurationChanged(newConfig);}@Overridepublic boolean onOptionsItemSelected(MenuItem item){// Pass the event to ActionBarDrawerToggle, if it returns// true, then it has handled the app icon touch eventif (mDrawerToggle.onOptionsItemSelected(item)){return true;}// Handle your other action bar items...return super.onOptionsItemSelected(item);}}
例子是從官方實例中扒出來的,比較糾結(jié)的是用了Level 11的一個API,這樣minSdkVersion就有限制,不能太低。
Or 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".DrawerActivity" ><android.support.v4.widget.DrawerLayoutandroid:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent" ><!-- The main content view --><FrameLayoutandroid:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="open" /></FrameLayout><!-- The navigation drawer --><ListViewandroid:id="@+id/left_drawer"android:layout_width="240dp"android:layout_height="match_parent"android:layout_gravity="start"android:background="#111"android:choiceMode="singleChoice"android:divider="@android:color/transparent"android:dividerHeight="0dp" /></android.support.v4.widget.DrawerLayout></RelativeLayout>
public class DrawerActivity extends Activity {private DrawerLayout mDrawerLayout = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_drawer);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);Button button = (Button) findViewById(R.id.btn);button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 按鈕按下,將抽屜打開 mDrawerLayout.openDrawer(Gravity.LEFT);}});}}
我是天王蓋地虎的分割線?????????????????????????????????????????????????????????????
?
?
參考:http://www.cnblogs.com/mengdd/p/3213378.html
轉(zhuǎn)載于:https://www.cnblogs.com/yydcdut/p/3952670.html
總結(jié)
以上是生活随笔為你收集整理的Android -- DrawerLayout的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不知道电影名字
- 下一篇: C#编写的多生产者多消费者同步问题