android中的toolbar使用

1:导入V7包

右键项目 ->open module setting ->Dependencies ->点击+号-> 选择Library Dependency-> 搜索“appcompat-v7”

打开app/build.gradle,发现已经成功引入

图1.1

图1.2

图1.3

 

图1.4

 2:activity_main中修改文件

网上一般都采用了相对布局的方法,不知道原因,猜测是因为这样的话就可以实现actionbar和toolbar共存并放在想放的位置上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:logo="@mipmap/app_logo"
        app:title="快速登录注册"
        app:titleTextColor="#fff"
        app:navigationIcon="@drawable/ic_menu"
        android:theme="@style/Base.Theme.AppCompat.Light"
        >
    </androidx.appcompat.widget.Toolbar>
</RelativeLayout> 

3:Mainactivity.java

在多次尝试后,发现一件有趣的事情

Toolbar有两个,如果引入错误的话就会无法使用,同时需要和之前在配置文件中的toolbar相同类型,不然就会出现一个android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar,大意就是找不到这个类(用3.2的写法,会报如3.3所示的错误)

 

 

图3.1

 

 

 图3.2

图3.3

 

 

 

posted @   魏晋南北朝  阅读(3050)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示