Android ToolBar 学习笔记(2)

简单ToolBar+滑动菜单的实现

  1. 编写activity_main.xml

    DrawerLayout是AndroidX库中提供的一个布局控件,此布局允许放入2个直接控件,第一个控件是主屏幕中显示的内容、第二个控件是滑动菜单中显示的内容。

    1.1 使用 DrawerLayout 布局

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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">

1.2 第一个控件(主屏幕中显示的内容)

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tool_bar"
            xmlns:app="http //schemas android com/apk/res-auto"
            app:navigationIcon="@drawable/navi"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/purple_200"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            ></androidx.appcompat.widget.Toolbar>

    </FrameLayout>

1.3 第二个控件 (滑动菜单中显示的内容)

<TextView
            android:id="@+id/menu_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="@string/menu_title"
            android:textStyle="bold"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="200dp"
            android:background="@drawable/btn_style"
            android:textSize="30sp"
            ></TextView>
<!--    
android:layout_gravity="start"
告诉滑动菜单在左在右
start 属性自动判断
英语、汉语,滑动菜单在左边-->

此时,左滑已经可以看到滑动菜单(最左侧)

  1. toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu 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">

    <item
        android:id="@+id/love"
        android:icon="@drawable/love"
        android:title="@string/love"
        app:showAsAction="always"
        tools:ignore="MenuTitle"></item>
    <item
        android:id="@+id/save"
        android:icon="@drawable/save"
        android:title="@string/save"
        app:showAsAction="ifRoom"
        tools:ignore="MenuTitle"></item>
    <item
        android:id="@+id/rew"
        android:icon="@drawable/rew"
        android:title="@string/rewrite"
        app:showAsAction="never"
        tools:ignore="MenuTitle"></item>

<!--showAsAction的参数:
    always 永远显示在toolbar中
    ifRoom 屏幕空间足够就显示在toolbar中
    never 永远显示在菜单中   -->
</menu>

3.编写MainActivity

	private Toolbar tool_bar;
    private DrawerLayout drawerLayout;
    private TextView menu_title;

	private void init() {
        //绑定 DrawerLayout
        drawerLayout = findViewById(R.id.drawer_layout);

        //绑定 Toolbar
        tool_bar=findViewById(R.id.tool_bar);

        //得到toolBar实例
        setSupportActionBar(tool_bar);

        //显示 tool_bar左侧按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //设置 tool_bar左侧按钮图标
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.navi);

        menu_title=findViewById(R.id.menu_title);
    }

	//加载 toorlbar.xml这个菜单文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    //处理多个按钮的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                //按下home按钮 打开滑动菜单
                //openDrawer() 传入gravity参数 确保这里的行为和xml中定义的一致
                drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.love:
                Toast.makeText(this,"you clicked love",Toast.LENGTH_SHORT).show();
                break;
            case R.id.save:
                Toast.makeText(this,"you clicked save",Toast.LENGTH_SHORT).show();
                break;
            case R.id.rew:
                Toast.makeText(this,"you clicked rewrite",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值