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

怎么樣?是不是在平時(shí)開發(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è)滑動(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="左邊菜單測試" /> </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="右邊菜單測試" /> </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布局類似一個(gè)大容器,超屏布局,將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 {
// 抽屜菜單對(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è)置開關(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è)滑布局顯示時(shí)內(nèi)容之外區(qū)域的背景顏色,默認(rèn)是灰色,這里我為了大家看著清晰就設(shè)置成透明的了;二是drawerLayout的監(jiān)聽器ActionBarDrawerToggle,而ActionBarDrawerToggle對(duì)象我們通過查閱ActionBarDrawerToggle的源碼發(fā)現(xiàn)它是DrawerListener的實(shí)現(xiàn)類,也就是說ActionBarDrawerToggle通過實(shí)現(xiàn)DrawerListener監(jiān)聽,在此基礎(chǔ)上封裝了onDrawerOpened、onDrawerClosed、onDrawerStateChanged和onDrawerSlide的事件處理,以便于開發(fā)者在滑動(dòng)過程中自定義要處理的一些操作。
最后檢查一下你的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抽屜效果的菜單編寫實(shí)例
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android中DrawerLayout+ViewPager滑動(dòng)沖突的解決方法
- Android組件之DrawerLayout實(shí)現(xiàn)抽屜菜單
- Android中DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
- Android抽屜布局DrawerLayout的簡單使用
相關(guān)文章
Android實(shí)現(xiàn)頁面跳轉(zhuǎn)的全過程記錄
對(duì)于android軟件開發(fā)初級(jí)學(xué)習(xí)者來說,簡單的頁面跳轉(zhuǎn)是必學(xué)的,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)頁面跳轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Android重復(fù)引用多個(gè)場景報(bào)錯(cuò)的問題解決
本文主要介紹了Android重復(fù)引用多個(gè)場景報(bào)錯(cuò)的問題解決,主要介紹了5種情況,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間(示例代碼)
這篇文章主要介紹了Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間的示例代碼,需要的朋友可以參考下2017-05-05
Android Lottie實(shí)現(xiàn)中秋月餅變明月動(dòng)畫特效實(shí)例
Lottie是Airbnb開源的一個(gè)支持 Android、iOS 以及 ReactNative,利用json文件的方式快速實(shí)現(xiàn)動(dòng)畫效果的庫,下面這篇文章主要給大家介紹了關(guān)于Android Lottie實(shí)現(xiàn)中秋月餅變明月動(dòng)畫特效的相關(guān)資料,需要的朋友可以參考下2021-09-09
Android實(shí)現(xiàn)驗(yàn)證碼登錄
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)驗(yàn)證碼登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03

