android側(cè)滑菜單控件DrawerLayout使用方法詳解
drawerLayout是Support Library包中實(shí)現(xiàn)了側(cè)滑菜單效果的控件,可以說drawerLayout是因?yàn)榈谌娇丶鏜enuDrawer等的出現(xiàn)之后,google借鑒而出現(xiàn)的產(chǎn)物。drawerLayout分為側(cè)邊菜單和主內(nèi)容區(qū)兩部分,側(cè)邊菜單可以根據(jù)手勢(shì)展開與隱藏(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é)打開、關(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軸的順序來安排的 同時(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。為了滿足從右到左的約定,聲明它的值為”start” 代替 “l(fā)eft”(因此這個(gè)抽屜將會(huì)在右面呈現(xiàn)當(dāng)布局是RTL時(shí))
- 在導(dǎo)航View聲明時(shí):寬度用dp為單位、高度匹配父View。為了保證用戶無論怎樣都能看到主內(nèi)容的一部分,導(dǎo)航抽屜的寬度不能超過320dp
初始化Drawer List
在你的Activity中,要做的第一件事是初始化導(dǎo)航抽屜的列表項(xiàng)。具體該怎么做根據(jù)你APP的內(nèi)容來定,但是導(dǎo)航抽屜通常包含一個(gè)Listview,所以還需要一個(gè)相匹配的Adapter(比如 ArrayAdapter 或者 SimpleCursorAdapter)
下面的例子,告訴你該如何借助一個(gè)string array 來初始化一個(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)用戶選擇了抽屜列表里面的一個(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);
}
打開和關(guān)閉抽屜
使用onDrawerOpened()和onDrawerClosed () 打開和關(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 下載
以上就是本文的全部內(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中Fragment的分屏顯示處理橫豎屏顯示的實(shí)現(xiàn)方法
今天小編就為大家分享一篇關(guān)于Android中Fragment的分屏顯示處理橫豎屏顯示的實(shí)現(xiàn)方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
ScrollView與ListView合用(正確計(jì)算Listview的高度)的問題解決
最近做項(xiàng)目中用到ScrollView和ListView一起使用的問題,顯示的時(shí)候ListView不能完全正確的顯示,查了好多資料終于成功解決:2013-05-05
Android通過原生APi獲取所在位置的經(jīng)緯度
本篇文章主要介紹了Android通過原生APi獲取所在位置的經(jīng)緯度,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(九)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第九篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
React Native 實(shí)現(xiàn)熱更新并自動(dòng)簽名打包功能
這篇文章主要介紹了React Native 實(shí)現(xiàn)熱更新并自動(dòng)簽名打包,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
使用RecyclerView添加Header和Footer的方法
RecyclerView雖然作為ListView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒有提供,比如我們平時(shí)會(huì)經(jīng)常用到的addHeaderView,addFooterView,既然RecyclerView沒有提供這個(gè)方法,我們應(yīng)該如何為列表添加頭部和底部呢,接下來通過本文給大家介紹2016-03-03
Android Studio中CodeStyle模板的配置方式
這篇文章主要介紹了Android Studio中CodeStyle模板的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
2021最新Android筆試題總結(jié)美團(tuán)Android崗職能要求
這篇文章主要介紹了2021最新Android筆試題總結(jié)以及美團(tuán)Android崗職能要求,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
本篇文章主要介紹了Android記住密碼和自動(dòng)登錄界面的實(shí)現(xiàn)(SharedPreferences),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Android studio 項(xiàng)目手動(dòng)在本地磁盤中刪除module后,殘留文件夾無法刪除的問題解決方法
這篇文章主要介紹了Android studio 項(xiàng)目手動(dòng)在本地磁盤中刪除module后,殘留文件夾無法刪除問題,本文給出了解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

