安卓ToolBar+抽屉

前言

抽屉是生活中最常见的存放东西的容器,无论衣柜,茶几还是冰箱,都有抽屉的应用,他可以将空间很好的隐藏,不影响外部的美观还会节省空间,在安卓开发中也有抽屉的应用。

抽屉效果(此为示例图,百度搜的!!)
在这里插入图片描述

ToolBar布局代码

在这里插入图片描述

<android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/toolbar"
        android:background="@android:color/holo_blue_bright"
        app:logo="@mipmap/ic_launcher_round"//设置logo图片
        app:title="标题"
        app:titleTextColor="@android:color/holo_orange_light"
        app:subtitle="副标题"
        app:subtitleTextColor="@android:color/darker_gray"
        app:navigationIcon="@mipmap/ic_launcher_round"
        >
        </android.support.v7.widget.Toolbar>

ToolBarjava代码

public void init(){
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);//替换掉系统默认的toolbar
        //导航按钮添加点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Activity_day03.this, "这是导航按钮", Toast.LENGTH_SHORT).show();
            }
        });

    }

效果图:

在这里插入图片描述

抽屉+ToolBar布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.bw.day03.Activity_day03">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/toolbar"
        android:background="@android:color/holo_blue_bright"
        app:logo="@mipmap/ic_launcher_round"
        app:title="标题"
        app:titleTextColor="@android:color/holo_orange_light"
        app:subtitle="副标题"
        app:subtitleTextColor="@android:color/darker_gray"
        app:navigationIcon="@mipmap/ic_launcher_round"
        >
            <RelativeLayout
                android:layout_width="200dp"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入搜索内容"
                    android:drawableLeft="@android:drawable/ic_menu_search"
                    />
            </RelativeLayout>
    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer"
        android:layout_below="@id/toolbar"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="主界面"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#fff"
            >

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

在这里插入图片描述

java代码:

public void initDrawer(){
        drawer = findViewById(R.id.drawer);
        //绑定toolbar和drawerlayout
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this
                ,drawer
                ,toolbar
                ,R.string.openlayout
                ,R.string.closelayout);
        toggle.syncState();
        drawer.addDrawerListener(toggle);
    }

主类代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day03);
        init();//初始化
        initDrawer();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值