android側(cè)滑菜單控件DrawerLayout使用方法詳解
drawerLayout是Support Library包中實(shí)現(xiàn)了側(cè)滑菜單效果的控件,可以說(shuō)drawerLayout是因?yàn)榈谌娇丶鏜enuDrawer等的出現(xiàn)之后,google借鑒而出現(xiàn)的產(chǎn)物。drawerLayout分為側(cè)邊菜單和主內(nèi)容區(qū)兩部分,側(cè)邊菜單可以根據(jù)手勢(shì)展開(kāi)與隱藏(drawerLayout自身特性),主內(nèi)容區(qū)的內(nèi)容可以隨著菜單的點(diǎn)擊而變化(這需要使用者自己實(shí)現(xiàn))。
使用步驟:
創(chuàng)建一個(gè)DrawerLayout
為了添加導(dǎo)航抽屜,你需要在你的布局界面中聲明一個(gè)DrawerLayout對(duì)象作為布局的根節(jié)點(diǎn)。同時(shí)在DrawerLayout內(nèi)部添加兩個(gè)view:
- 添加一個(gè)View,它包含應(yīng)用的主內(nèi)容(當(dāng)抽屜隱藏時(shí)你的主要布局);
- 添加另一個(gè)View它包含了導(dǎo)航抽屜;
如下面例子所示:該布局使用了DrawerLayout它包含了兩個(gè)子節(jié)點(diǎn):一個(gè)FrameLayout它包含了主要內(nèi)容(在運(yùn)行時(shí)將會(huì)被Fragment替換) 和 一個(gè)ListView作為導(dǎo)航抽屜,上面titlebar 上圖標(biāo),負(fù)責(zé)打開(kāi)、關(guān)閉抽屜;
<?xml version="1.0" encoding="utf-8"?> <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" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:id="@+id/titleBar" android:gravity="center_vertical" android:background="@android:color/darker_gray" android:layout_height="40dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_drawer" android:onClick="onClickDrawerOpened" android:clickable="true" android:id="@+id/imageView" /> </LinearLayout> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_below="@id/titleBar" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <!-- The navigation drawer --> <ListView android: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>
上面這個(gè)例子包含了一些重要的布局技巧:
- 主內(nèi)容View(FrameLayout在最上層)必須是Drawerlayout的第一個(gè)子節(jié)點(diǎn)因?yàn)閄ML在安排這些界面的時(shí)候是按照Z(yǔ)軸的順序來(lái)安排的 同時(shí) 抽屜必須在主內(nèi)容的頂部。
- 主內(nèi)容View被設(shè)置成匹配父View的寬和高,因?yàn)楫?dāng)導(dǎo)航抽屜隱藏的時(shí)候它要填充整個(gè)UI。
- 導(dǎo)航View(ListView)必須被聲明一個(gè)水平的gravity借助屬性android:layout_gravity。為了滿(mǎn)足從右到左的約定,聲明它的值為”start” 代替 “l(fā)eft”(因此這個(gè)抽屜將會(huì)在右面呈現(xiàn)當(dāng)布局是RTL時(shí))
- 在導(dǎo)航View聲明時(shí):寬度用dp為單位、高度匹配父View。為了保證用戶(hù)無(wú)論怎樣都能看到主內(nèi)容的一部分,導(dǎo)航抽屜的寬度不能超過(guò)320dp
初始化Drawer List
在你的Activity中,要做的第一件事是初始化導(dǎo)航抽屜的列表項(xiàng)。具體該怎么做根據(jù)你APP的內(nèi)容來(lái)定,但是導(dǎo)航抽屜通常包含一個(gè)Listview,所以還需要一個(gè)相匹配的Adapter(比如 ArrayAdapter 或者 SimpleCursorAdapter)
下面的例子,告訴你該如何借助一個(gè)string array 來(lái)初始化一個(gè)導(dǎo)航list
public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private String[] mPlanetTitles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // set up the drawer's list view with items and click listener mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); } //................................ }
處理導(dǎo)航點(diǎn)擊事件
當(dāng)用戶(hù)選擇了抽屜列表里面的一個(gè)Item時(shí), 系統(tǒng)調(diào)用onItemClickListener上的onItemClick(), 給setOnItemClickListener().
你在onItemClick()方法里面做什么, 取決于你的app實(shí)現(xiàn)的結(jié)構(gòu). 在下面的例子中, 選擇每一個(gè)Item都會(huì)在主要內(nèi)容的布局中插入一個(gè)不同的Fragment.
private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } } private void selectItem(int position) { // update the main content by replacing fragments Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerLayout.closeDrawer(mDrawerList); }
打開(kāi)和關(guān)閉抽屜
使用onDrawerOpened()和onDrawerClosed () 打開(kāi)和關(guān)閉抽屜:
public void onClickDrawerOpened(View drawerView) { if(!mDrawerLayout.isDrawerOpen(GravityCompat.START))//if not open ,open or close; { mDrawerLayout.openDrawer(mDrawerList); } else { mDrawerLayout.closeDrawer(mDrawerList); } }
效果圖:
Demo 下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android自定義實(shí)現(xiàn)側(cè)滑菜單效果
- Android實(shí)現(xiàn)QQ側(cè)滑菜單效果
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android使用DrawerLayout實(shí)現(xiàn)雙向側(cè)滑菜單
- Android中DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
- Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果
- android實(shí)現(xiàn)左右側(cè)滑菜單效果
- Android使用DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
- Android實(shí)現(xiàn)側(cè)滑菜單DrawerLayout
相關(guān)文章
Android編程實(shí)現(xiàn)提取網(wǎng)址鏈接的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)提取網(wǎng)址鏈接的方法,涉及Android針對(duì)字符串的正則匹配操作相關(guān)技巧,需要的朋友可以參考下2016-10-10解決Android SearchView不顯示搜索icon的問(wèn)題
這篇文章主要介紹了解決Android SearchView不顯示搜索icon問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Android Java實(shí)現(xiàn)余弦匹配算法示例代碼
這篇文章主要介紹了Android Java實(shí)現(xiàn)余弦匹配算法示例代碼的相關(guān)資料,這里通過(guò)java 算法實(shí)現(xiàn)余弦匹配算法實(shí)現(xiàn)比較的實(shí)例,需要的朋友可以參考下2016-11-11Android Studio 多層級(jí) Module 對(duì) aar 引用問(wèn)題解決方法
這篇文章主要介紹了Android Studio 多層級(jí) Module 對(duì) aar 引用問(wèn)題的解決方法,需要的朋友參考下2017-12-12