自定义控件之DrawerLayout实现侧滑

DrawerLayout可以实现抽屉式效果:

实现代码:

activity_main.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/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mhy.zidingyiview.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:onClick="left"
            android:id="@+id/left_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="显示左边侧滑栏" />

        <Button
            android:onClick="right"
            android:id="@+id/right_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="显示右边边侧滑栏" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_left"
       <strong><span style="font-size:18px;color:#ff0000;"> android:layout_gravity="left"</span></strong>
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/pink"
        android:gravity="center">

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="阅读"
            android:textSize="28sp"/>
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跑步"
            android:textSize="28sp"/>
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="观赏"
            android:textSize="28sp"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_right"
        <strong><span style="font-size:18px;color:#ff0000;">android:layout_gravity="right"</span></strong>
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#509da6">
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
MainActivity.java代码

package com.example.mhy.zidingyiview;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayer;
    private Button mLeftSlideButton;
    private Button mRightSlideButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayer = (DrawerLayout) findViewById(R.id.drawerLayout);
        mLeftSlideButton = (Button)findViewById(R.id.left_btn);
        mRightSlideButton = (Button)findViewById(R.id.right_btn);
    }

    public void left(View v) {
        //打开右边的抽屉
        mDrawerLayer.openDrawer(Gravity.LEFT);
        //关闭右边的抽屉
//        mDrawerLayer.closeDrawer(Gravity.LEFT);
    }

    public void right(View v) {
        mDrawerLayer.openDrawer(Gravity.RIGHT);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值