Toolbar+DrawerLayout简单简介和简单使用方法

本文简要介绍了Android中的Toolbar和DrawerLayout组件,并详细阐述了它们的基本使用方法。通过布局文件配置和Menu资源,展示了如何创建一个具备侧滑菜单功能的Android应用界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toolbar+DrawerLayout

	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

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

布局文件

<?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=".Day03.ToolBarTestActivity">

    <android.support.v7.widget.Toolbar
        app:navigationIcon="@drawable/ic_launcher_background"
        app:subtitleTextColor="#4f5"
        app:subtitle="副标题"
        app:titleTextColor="#f40"
        app:title="标题"
        app:logo="@mipmap/ic_launcher_round"
        android:background="#f95"
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="100dp"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout
        android:layout_below="@id/tb"

        android:id="@+id/dl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:background="#65f"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="这是主界面" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="200dp"

            android:layout_gravity="left"
            android:layout_height="match_parent">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="左侧视图"
                />
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Menu文件下的Menu布局

<?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">

    <item android:id="@+id/item1" android:title="菜单" app:showAsAction="ifRoom">
        <menu>
            <item android:id="@+id/subMenu" android:title="子菜单" app:showAsAction="ifRoom"></item>
        </menu>
    </item>
</menu>
package com.example.sixeightwork.Day03;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sixeightwork.R;

public class ToolBarTestActivity extends AppCompatActivity {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    TextView textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_bar_test);

        initview();

        initDarerLayout();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

       getMenuInflater().inflate(R.menu.toolbar_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        return super.onOptionsItemSelected(item);
    }

    public void initview(){
        textView=findViewById(R.id.textv);
        toolbar=findViewById(R.id.tb);
        drawerLayout=findViewById(R.id.dl);
        setSupportActionBar(toolbar);//让他替换actionbar
        //导航按钮添加点击事件
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ToolBarTestActivity.this, "点击了导航按钮", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void initDarerLayout(){


        //绑定toolbar和drawerLayout
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openlayout,R.string.closelayout);

        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值