DrawerLayout实现侧滑仿QQ界面

简介

可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。
drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),
主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)。


1.编写Activity的布局文件
根布局使用android.support.v4.widget.DrawerLayout
然后其内部第一个View为内容区域,第二个View为左侧菜单,第三个View为右侧侧滑菜单,当前第三个是可选的。
第一个View的宽高应当设置为match_parent,当然了,这也理所当然。
第二、三个View需要设置Android:layout_gravity=”left”,和android:layout_gravity=”right”且一搬高度设置为match_parent,
宽度为固定值,即侧滑菜单的宽度(宽度你写match_parent也行,但是不会全部覆盖)。
首先是mainactivity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mdrawerLayout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.soft.qianyu.myqqdddreawerlayout.MainActivity">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:background="#60f227"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="我是内容界面"/>

        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开侧滑菜单"
            />
    </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:background="#4b91f9"
        android:id="@+id/left"
        android:layout_width="match_parent"
        android:layout_gravity="left"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="我是侧滑菜单"/>

        <Button
            android:id="@+id/bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回"
            />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

写完布局之后,你就可以实现侧滑了!但是此时的侧滑并不是将内容界面挤到右侧的侧滑,而是覆盖在内容界面上面的侧滑。此时若还要实现像QQ那样的侧滑,则还需要以下代码。

2,在代码中为相应的控件添加事件监听器

public class MainActivity extends Activity implements View.OnClickListener{
    //1)声明DrawerLayout变量
    DrawerLayout drawerLayout;
    //声明内容页面变量
    LinearLayout contentView ;
    //声明按钮
    Button bt1,bt2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        init();
    }

private void init() {
//2)实例化DrawerLayout控件
drawerLayout = (DrawerLayout) findViewById(R.id.mdrawerLayout);
//实例化按钮并设置点击监听事件
findViewById(R.id.bt1).setOnClickListener(MainActivity.this);
findViewById(R.id.bt2).setOnClickListener(MainActivity.this);
//3)为DrawerLayout控件添加监听器
drawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            //当侧滑菜单正在滑动时触发的方法
            /**
             第一个参数:正在滑动的侧滑菜单
             第二个参数:菜单滑动的宽度的百分比
             **/
            @Override
public void onDrawerSlide(View drawerView, float slideOffset) {
       super.onDrawerSlide(drawerView, slideOffset);
//获得侧滑菜单的宽度
int drawerViewWidth = drawerView.getMeasuredWidth();
//根据滑动百分比计算内容部分应该向右边移动的距离
int marginLeft = (int)(drawerViewWidth * slideOffset) ;
//获得内容部分的View对象(内容View对象是第一个,所以是0)
contentView = (LinearLayout) drawerLayout.getChildAt(0);
//修改内容部分的左边距
contentView.setLeft(marginLeft);
      }
  });
}

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt1:
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
            case R.id.bt2:
                drawerLayout.closeDrawers();
                break;
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现Android侧滑菜单有多种方式,其中一种比较常见的实现方式是使用DrawerLayout和NavigationView。 步骤如下: 1. 在XML布局文件中添加DrawerLayout和NavigationView,其中NavigationView中可以添加菜单项。 ``` <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 主界面内容 --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 侧滑菜单 --> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header" app:menu="@menu/nav_menu" /> </android.support.v4.widget.DrawerLayout> ``` 2. 在Activity中设置DrawerLayout和NavigationView的监听器,并在onOptionsItemSelected方法中处理菜单项点击事件。 ``` public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private NavigationView mNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = findViewById(R.id.drawer_layout); mNavigationView = findViewById(R.id.navigation_view); mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { // 处理菜单项点击事件 return false; } }); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.addDrawerListener(toggle); toggle.syncState(); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { mDrawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } } ``` 3. 在NavigationView中添加菜单项,并为菜单项设置图标和标题。 ``` <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_gallery" android:title="Gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_slideshow" android:title="Slideshow" /> </group> </menu> ``` 至此,实现了一个简单的Android侧滑菜单。如果要实现仿QQ侧滑删除功能,可以在ListView或RecyclerView中添加滑动删除的功能,并在删除时更新侧滑菜单中的未读消息数等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值