国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android原生側滑控件DrawerLayout使用方法詳解

2019-10-22 18:21:39
字體:
來源:轉載
供稿:網友

在android的v4包中有一個控件 Drawerlayout,主要實現了左拉和右拉菜單,類似于之前的“抽屜”功能,此控件使用簡單,效果很柔和,操作起來體驗非常好,下面是我實現的一個簡單效果的部分截圖:

左拉:

Android,側滑控件,DrawerLayout

右拉:

Android,側滑控件,DrawerLayout

怎么樣?是不是在平時開發的應用中很常見?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"> <!-- 下面顯示的主要是主界面內容 --> <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> <!-- 左側滑動欄 --> <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> <!-- 右側滑動欄 --> <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>

通過上面的布局文件我們發現 drawerlayout中的子布局分為content、left、right三部分,其中left和right的布局需要在layout中聲明android:layout_gravity屬性,值分別是start和end。很顯然,drawerlayout布局類似一個大容器,超屏布局,將left的布局放在了控件的開始地方,right的布局放在了控件結尾的地方。

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);  //設置菜單內容之外其他區域的背景色  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); } //設置開關監聽 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);   }   // 菜單關閉   @Override   public void onDrawerClosed(View drawerView) {    super.onDrawerClosed(drawerView);   }  };  drawerLayout.setDrawerListener(drawerbar); } //左邊菜單開關事件 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);  } } // 右邊菜單開關事件 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),此屬性設置的是側滑布局顯示時內容之外區域的背景顏色,默認是灰色,這里我為了大家看著清晰就設置成透明的了;二是drawerLayout的監聽器ActionBarDrawerToggle,而ActionBarDrawerToggle對象我們通過查閱ActionBarDrawerToggle的源碼發現它是DrawerListener的實現類,也就是說ActionBarDrawerToggle通過實現DrawerListener監聽,在此基礎上封裝了onDrawerOpened、onDrawerClosed、onDrawerStateChanged和onDrawerSlide的事件處理,以便于開發者在滑動過程中自定義要處理的一些操作。

最后檢查一下你的AS是不是最新版,如果不是的話,則需要在build.gradle中增加以下配置:

compile 'com.android.support:appcompat-v7:24.2.1'


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荃湾区| 舒城县| 临朐县| 黔江区| 六盘水市| 宜丰县| 缙云县| 中牟县| 平阳县| 阳谷县| 博爱县| 日土县| 从江县| 天门市| 张北县| 班戈县| 电白县| 进贤县| 察哈| 大安市| 惠州市| 江源县| 九龙县| 珲春市| 内江市| 五峰| 东乌珠穆沁旗| 赞皇县| 永和县| 昆山市| 鸡西市| 甘泉县| 恩平市| 南昌市| 荥经县| 松桃| 永顺县| 新郑市| 卢氏县| 荆州市| 鄂州市|