如何在Android Studio中导入工具栏工具? [英] How should I import Toolbar tool in android studio?
问题描述
我正在尝试将Toolbar
添加到我的android studio项目中.我找到了一些有关应该如何做的解释,并且都这些步骤进行了解释.第一步,我应该导入android.support.v7.widget.Toolbar;
,但不会成功:v7
字符用红色突出显示,警告消息是:无法解析符号'v7'".我试图用import android.support.____
之后的建议栏中包含的v4
替换v7
,但是当我这样做时,android studio声称它无法解析符号'widget'".
I am trying to add a Toolbar
to my android studio project. I found several explanations of how should I do it and they all explained these steps. In the first step, I should import android.support.v7.widget.Toolbar;
but it does not succeed: The v7
characters bulbing in red and the warning message is: "Cannot resolve symbol 'v7'". I tried to replace the v7
with v4
which is included in the suggestions bar after the import android.support.____
but when I do so, the android studio claims it "cannot resolve symbol 'widget'".
如何成功导入Toolbar
工具?
推荐答案
由于您使用的是Androidx库,因此您必须:
Since you are using Androidx libraries you have to:
- 在代码
import androidx.appcompat.widget.Toolbar
中使用正确的类. - 使用正确的AppCompativity:
import androidx.appcompat.app.AppCompatActivity;
- 在布局中使用
<androidx.appcompat.widget.Toolbar
- use the right class in your code
import androidx.appcompat.widget.Toolbar
. - use the right AppCompativity:
import androidx.appcompat.app.AppCompatActivity;
- use
<androidx.appcompat.widget.Toolbar
in your layout
如果您要使用材料组件库
- 添加库
implementation 'com.google.android.material:material:<version>'
- 按照入门页将主题更改为
Theme.MaterialComponents.*
主题 - 在代码和布局中使用
com.google.android.material.appbar.MaterialToolbar
- add the library
implementation 'com.google.android.material:material:<version>'
- follow the getting started page to change the theme to a
Theme.MaterialComponents.*
theme - Use the
com.google.android.material.appbar.MaterialToolbar
in your code and in your layout
这篇关于如何在Android Studio中导入工具栏工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!