Android原生側(cè)滑控件DrawerLayout使用方法詳解
在android的v4包中有一個(gè)控件 Drawerlayout,主要實(shí)現(xiàn)了左拉和右拉菜單,類(lèi)似于之前的“抽屜”功能,此控件使用簡(jiǎn)單,效果很柔和,操作起來(lái)體驗(yàn)非常好,下面是我實(shí)現(xiàn)的一個(gè)簡(jiǎn)單效果的部分截圖:
左拉:
右拉:
怎么樣?是不是在平時(shí)開(kāi)發(fā)的應(yīng)用中很常見(jiàn)?OK,那么接下來(lái)我直接上代碼:
activity_sliding.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent"> <!-- 下面顯示的主要是主界面內(nèi)容 --> <RelativeLayout android:id="@+id/main_content_frame_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:onClick="openLeftLayout" android:text="左邊" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="100dp" android:onClick="openRightLayout" android:text="右邊" /> </RelativeLayout> <!-- 左側(cè)滑動(dòng)欄 --> <RelativeLayout android:id="@+id/main_left_drawer_layout" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/colorPrimary" android:paddingTop="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="左邊菜單測(cè)試" /> </RelativeLayout> <!-- 右側(cè)滑動(dòng)欄 --> <RelativeLayout android:id="@+id/main_right_drawer_layout" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="end" android:background="@color/colorPrimary" android:paddingTop="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="右邊菜單測(cè)試" /> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
通過(guò)上面的布局文件我們發(fā)現(xiàn) drawerlayout中的子布局分為content、left、right三部分,其中l(wèi)eft和right的布局需要在layout中聲明android:layout_gravity屬性,值分別是start和end。很顯然,drawerlayout布局類(lèi)似一個(gè)大容器,超屏布局,將left的布局放在了控件的開(kāi)始地方,right的布局放在了控件結(jié)尾的地方。
DrawerSlidingActivity.java:
import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.RelativeLayout; public class DrawwerSlidingActivity extends AppCompatActivity { // 抽屜菜單對(duì)象 private ActionBarDrawerToggle drawerbar; public DrawerLayout drawerLayout; private RelativeLayout main_left_drawer_layout, main_right_drawer_layout; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.activity_slidingmenu); initLayout(); initEvent(); } public void initLayout() { drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout); //設(shè)置菜單內(nèi)容之外其他區(qū)域的背景色 drawerLayout.setScrimColor(Color.TRANSPARENT); //左邊菜單 main_left_drawer_layout = (RelativeLayout) findViewById(R.id.main_left_drawer_layout); //右邊菜單 main_right_drawer_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout); } //設(shè)置開(kāi)關(guān)監(jiān)聽(tīng) private void initEvent() { drawerbar = new ActionBarDrawerToggle(this, drawerLayout, R.mipmap.ic_launcher, R.string.open, R.string.close) { //菜單打開(kāi) @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } // 菜單關(guān)閉 @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } }; drawerLayout.setDrawerListener(drawerbar); } //左邊菜單開(kāi)關(guān)事件 public void openLeftLayout(View view) { if (drawerLayout.isDrawerOpen(main_left_drawer_layout)) { drawerLayout.closeDrawer(main_left_drawer_layout); } else { drawerLayout.openDrawer(main_left_drawer_layout); } } // 右邊菜單開(kāi)關(guān)事件 public void openRightLayout(View view) { if (drawerLayout.isDrawerOpen(main_right_drawer_layout)) { drawerLayout.closeDrawer(main_right_drawer_layout); } else { drawerLayout.openDrawer(main_right_drawer_layout); } } }
其中要注意的地方一是:drawerLayout.setScrimColor(Color.TRANSPARENT),此屬性設(shè)置的是側(cè)滑布局顯示時(shí)內(nèi)容之外區(qū)域的背景顏色,默認(rèn)是灰色,這里我為了大家看著清晰就設(shè)置成透明的了;二是drawerLayout的監(jiān)聽(tīng)器ActionBarDrawerToggle,而ActionBarDrawerToggle對(duì)象我們通過(guò)查閱ActionBarDrawerToggle的源碼發(fā)現(xiàn)它是DrawerListener的實(shí)現(xiàn)類(lèi),也就是說(shuō)ActionBarDrawerToggle通過(guò)實(shí)現(xiàn)DrawerListener監(jiān)聽(tīng),在此基礎(chǔ)上封裝了onDrawerOpened、onDrawerClosed、onDrawerStateChanged和onDrawerSlide的事件處理,以便于開(kāi)發(fā)者在滑動(dòng)過(guò)程中自定義要處理的一些操作。
最后檢查一下你的AS是不是最新版,如果不是的話,則需要在build.gradle中增加以下配置:
compile 'com.android.support:appcompat-v7:24.2.1'
- Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能
- Android使用DrawerLayout實(shí)現(xiàn)仿QQ雙向側(cè)滑菜單
- Android App中DrawerLayout抽屜效果的菜單編寫(xiě)實(shí)例
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android中DrawerLayout+ViewPager滑動(dòng)沖突的解決方法
- Android組件之DrawerLayout實(shí)現(xiàn)抽屜菜單
- Android中DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
- Android抽屜布局DrawerLayout的簡(jiǎn)單使用
相關(guān)文章
Android Studio實(shí)現(xiàn)華為手機(jī)的充電動(dòng)畫(huà)效果
本篇文章介紹了我參照華為手機(jī)的充電動(dòng)畫(huà)來(lái)仿照實(shí)現(xiàn)的樣例,這個(gè)動(dòng)畫(huà)并不難實(shí)現(xiàn),不過(guò)案例精簡(jiǎn)具有參考意義,需要的朋友快往下看吧2021-10-10android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件
本篇文章主要介紹了android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件,有興趣的同學(xué)可以了解一下。2016-11-11Android自定義View實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android實(shí)現(xiàn)水平帶刻度的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)水平帶刻度的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯
這篇文章主要介紹了Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯的相關(guān)資料,需要的朋友可以參考下2016-01-01android 跳轉(zhuǎn)到應(yīng)用通知設(shè)置界面的示例
本篇文章主要介紹了android 跳轉(zhuǎn)到應(yīng)用通知設(shè)置界面的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)
最近需要實(shí)現(xiàn)一個(gè)手機(jī)通訊錄的快速索引功能。根據(jù)姓名首字母快速索引功能,下面通過(guò)本篇文章給大家介紹Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)的相關(guān)代碼,需要的朋友參考下2015-12-12