温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何使用DrawerLayout侧滑控件

发布时间:2021-06-28 17:40:53 来源:亿速云 阅读:111 作者:Leah 栏目:移动开发

Android中如何使用DrawerLayout侧滑控件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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);
  }
 }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI