Toolbar+DrawerLayout SlideMenu 2种方式实现抽屉侧滑

一.ToolBar常用的方法
Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件,以此来取代之前的Actionbar 。我们需要在工程中引入appcompat-v7的兼容包以便向下兼容, 使用android.support.v7.widget.Toolbar进行开发。在设计 Toolbar 的时候,Google也留给了开发者很多可定制修改的余地,这些可定制修改的属性在API文档中都有详细介绍,如:

1.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题栏
2.Toolbar.setLogo(),设置logo图片;
3.Toolbar.setTitle().设置标题;
4.Toolbar.setSubTitle()设置子标题;
5.Toolbar.setTitleTextColor(int color);设置标题文字颜色;
6.Toolbar.setSubtitleTextColor();设置子标题文字颜色;
7.setTitleMargin(int start, int top, int end, int bottom);设置标题margin值; 8.onCreateOptionsMenu,getMenuInflater().inflate(R.menu.menu,menu)
设置菜单在给Toolbar设置为actionbar时使用;
9.Toolbar.setOnMenuItemClickListener();Toolbar绑定menu监听;
10.Toolbar.inflateMenu(R.menu.menu)在Toolbar没有替换actionbar时使用;
11.setSupportActionBar(mToolbar);设置toolbar替换actionbar;
12.getLayoutInflater().inflate(R.layout.view_tv,bar);Toolbar添加自定义view

二.DrawerLayout常用的方法
DrawerLayout.isDrawerOpen(Gravity.LEFT)是否开启;
DrawerLayout.openDrawer(Gravity.LEFT);开启抽屉
DrawerLayout.closeDrawer(Gravity.RIGHT);关闭抽屉

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            />

    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/draw"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


        </LinearLayout>
        <LinearLayout
            android:background="#ffffff"
            android:layout_gravity="left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
              <ImageView
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:src="@mipmap/ic_launcher"/>
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="旧梦"/>
          </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="拍摄我的状态"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="了解会员特权"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="QQ钱包"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="个人装饰"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="我的收藏"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="我的相册"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="我的文件"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="免流量特权"/>

            </LinearLayout>



        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>


</LinearLayout>
   Toolbar toolbar;
    ImageView imageView;
    DrawerLayout drawerLayout;
    TextView tv;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 tv=findViewById(R.id.tv);
 tv.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
         builder.setIcon(R.mipmap.ic_launcher);
         builder.setTitle("是否退出");
         builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "退出了", Toast.LENGTH_SHORT).show();
             }
         });
         builder.setNegativeButton("cencel", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
             }
         });
         Dialog dialog=builder.create();
         dialog.show();
     }
 });
         drawerLayout = findViewById(R.id.draw);



//        toolbar=findViewById(R.id.toolbar);
        imageView=findViewById(R.id.iv);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                drawerLayout.openDrawer(Gravity.LEFT);
            }
        });

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值