欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android原生側(cè)滑控件DrawerLayout使用方法詳解

 更新時(shí)間:2017年12月02日 10:03:30   作者:尹人入勝  
這篇文章主要為大家詳細(xì)介紹了Android原生側(cè)滑控件DrawerLayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在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'

相關(guān)文章

最新評(píng)論