Android原生側(cè)滑控件DrawerLayout使用方法詳解
在android的v4包中有一個控件 Drawerlayout,主要實現(xiàn)了左拉和右拉菜單,類似于之前的“抽屜”功能,此控件使用簡單,效果很柔和,操作起來體驗非常好,下面是我實現(xiàn)的一個簡單效果的部分截圖:
左拉:
右拉:

怎么樣?是不是在平時開發(fā)的應(yīng)用中很常見?OK,那么接下來我直接上代碼:
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è)滑動欄 --> <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="左邊菜單測試" /> </RelativeLayout> <!-- 右側(cè)滑動欄 --> <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="右邊菜單測試" /> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
通過上面的布局文件我們發(fā)現(xiàn) drawerlayout中的子布局分為content、left、right三部分,其中l(wèi)eft和right的布局需要在layout中聲明android:layout_gravity屬性,值分別是start和end。很顯然,drawerlayout布局類似一個大容器,超屏布局,將left的布局放在了控件的開始地方,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 {
// 抽屜菜單對象
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è)置開關(guān)監(jiān)聽
private void initEvent() {
drawerbar = new ActionBarDrawerToggle(this, drawerLayout, R.mipmap.ic_launcher, R.string.open, R.string.close) {
//菜單打開
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
// 菜單關(guān)閉
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
drawerLayout.setDrawerListener(drawerbar);
}
//左邊菜單開關(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);
}
}
// 右邊菜單開關(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è)滑布局顯示時內(nèi)容之外區(qū)域的背景顏色,默認是灰色,這里我為了大家看著清晰就設(shè)置成透明的了;二是drawerLayout的監(jiān)聽器ActionBarDrawerToggle,而ActionBarDrawerToggle對象我們通過查閱ActionBarDrawerToggle的源碼發(fā)現(xiàn)它是DrawerListener的實現(xiàn)類,也就是說ActionBarDrawerToggle通過實現(xiàn)DrawerListener監(jiān)聽,在此基礎(chǔ)上封裝了onDrawerOpened、onDrawerClosed、onDrawerStateChanged和onDrawerSlide的事件處理,以便于開發(fā)者在滑動過程中自定義要處理的一些操作。
最后檢查一下你的AS是不是最新版,如果不是的話,則需要在build.gradle中增加以下配置:
compile 'com.android.support:appcompat-v7:24.2.1'
- Android實現(xiàn)右邊抽屜Drawerlayout效果
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android DrawerLayout實現(xiàn)側(cè)拉菜單功能
- Android使用DrawerLayout實現(xiàn)仿QQ雙向側(cè)滑菜單
- Android App中DrawerLayout抽屜效果的菜單編寫實例
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android中DrawerLayout+ViewPager滑動沖突的解決方法
- Android組件之DrawerLayout實現(xiàn)抽屜菜單
- Android中DrawerLayout實現(xiàn)側(cè)滑菜單效果
- Android抽屜布局DrawerLayout的簡單使用
相關(guān)文章
Android Studio實現(xiàn)華為手機的充電動畫效果
本篇文章介紹了我參照華為手機的充電動畫來仿照實現(xiàn)的樣例,這個動畫并不難實現(xiàn),不過案例精簡具有參考意義,需要的朋友快往下看吧2021-10-10
android為ListView每個Item上面的按鈕添加事件
本篇文章主要介紹了android為ListView每個Item上面的按鈕添加事件,有興趣的同學可以了解一下。2016-11-11
Android自定義View實現(xiàn)抽獎轉(zhuǎn)盤
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)抽獎轉(zhuǎn)盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
android 跳轉(zhuǎn)到應(yīng)用通知設(shè)置界面的示例
本篇文章主要介紹了android 跳轉(zhuǎn)到應(yīng)用通知設(shè)置界面的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

