android.support.v7.widget.toolbar 在使用 android studio 的 java 文件中找不到?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36233798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:36:32  来源:igfitidea点击:

android.support.v7.widget.toolbar is not found in java file using android studio?

javaandroidxmlandroid-toolbar

提问by Manish Tiwari

I have created a toolbar xml file and want to add in MainActivity.

我创建了一个工具栏 xml 文件并想添加到 MainActivity 中。

i have include toolbar.xml file in MainActivity layout file

我在 MainActivity 布局文件中包含了 toolbar.xml 文件

Toolbar.xml code:

Toolbar.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/dark_blue">

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

Main Activity Xml Layout :

主要活动 Xml 布局:

<?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"
    tools:context="hogwart.harrypotter.bucketdrop.ActivityMain">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
    <include layout="@layout/empty_drops"/>
</RelativeLayout>

Then i want to add toolbar to main activity via java code but here it shows one toolbar :

然后我想通过 java 代码将工具栏添加到主要活动,但这里显示了一个工具栏:

import android.widget.Toolbar;

But support toolbar is not found :

但是没有找到支持工具栏:

import android.support.v7.widget.Toolbar;

Main Activity Code:

主要活动代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toolbar;

public class ActivityMain extends AppCompatActivity {

    Toolbar
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Gradle file code:

Gradle文件代码:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "hogwart.harrypotter.bucketdrop"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
}

回答by Joel Min

Add the following dependency in your gradle:

在您的 gradle 中添加以下依赖项:

compile 'com.android.support:appcompat-v7:23.1.1'

This library has the support toolbar you are looking for. If this still does not solve the problem, try invalidating cache and restart android studio.

该库具有您正在寻找的支持工具栏。如果这仍然不能解决问题,请尝试使缓存无效并重新启动 android studio。

回答by IntelliJ Amiya

You can add ProperApp CompactV7 support support librarydependency to your application via build.gradlefile.

您可以通过build.gradle文件向您的应用程序添加适当的App Compact V7 支持支持库依赖

You should call

你应该打电话

 compile 'com.android.support:appcompat-v7:23.1.1'

Then

然后

  1. Clean project
  2. Rebuild project
  3. Sync Gradle
  1. 清洁工程
  2. 重建项目
  3. 同步摇篮

回答by Gundu Bandgar

Add below code in your dependency.

在您的依赖项中添加以下代码。

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.0.1'
}

And this toolbar

还有这个工具栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:local="http://schemas.android.com/apk/res-auto"
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:minHeight="?attr/actionBarSize"
 android:background="#1b5e20"
 local:theme="@style/ThemeOverlay.AppCompat.Dark"
 local:popupTheme="@style/Theme.AppCompat.Light.NoActionBar"
>

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

回答by M. M. Human

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

Reminder: Please check maybe your theme is selected to "NoActionBar" try changing it to AppTheme

提醒:请检查您的主题是否已选择为“NoActionBar”,请尝试将其更改为AppTheme

enter image description here

在此处输入图片说明

回答by fshkntu

replace import android.support.v7.widget.Toolbar;with import androidx.appcompat.widget.Toolbar;.

替换import android.support.v7.widget.Toolbar;import androidx.appcompat.widget.Toolbar;.

回答by Anton Makov

If you are trying to convert support views like ConstraintLayout ViewPager from support v4/v7 to androidx and you don't know the package name of every view, just press Crtl + space

如果您尝试将诸如 ConstraintLayout ViewPager 之类的支持视图从支持 v4/v7 转换为 androidx 并且您不知道每个视图的包名称,只需按 Crtl + 空格

Example

例子

     <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

             ...

</android.support.constraint.ConstraintLayout>

All you need to do is to delete the package name of the view (in this case android.support.constraint) and just put the cursor after the name ConstraintLayout and press Crtl + space and it will auto complete the package for you.

您需要做的就是删除视图的包名称(在本例中为 android.support.constraint),只需将光标放在名称 ConstraintLayout 之后,然后按 Crtl + 空格,它就会自动为您完成包。

To this:

对此:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">


   ...
</androidx.constraintlayout.widget.ConstraintLayout>

This solution might be trivial for some people but I figured it would be helpful for others that don't know this.

这个解决方案对某些人来说可能是微不足道的,但我认为这对不知道这一点的其他人会有所帮助。

回答by Komarov Andrey

I have a same problem and i fix it by correct paths:

我有同样的问题,我通过正确的路径修复它:

my toolbar.xml was placed in values/toolbar.xml, and correctm path is the: layout/toolbar.xml

我的toolbar.xml 被放置在values/toolbar.xml 中,并且更正路径是:layout/toolbar.xml

回答by Mohamed G. Yaseen

Wherever you want to use it in XML or in java instead of android.support.v7.widget.Toolbaruse androidx.appcompat.widget.Toolbarand it should work just fine

无论您想在 XML 中还是在 java 中android.support.v7.widget.Toolbar使用androidx.appcompat.widget.Toolbar它而不是使用它,它都应该可以正常工作

回答by canerkaseler

Maybe, you are using AndroidX. Please check it. Also, you should update your libraries for AndroidX. For this you should use 29 version.

也许,您正在使用 AndroidX。请检查一下。此外,您应该为 AndroidX 更新您的库。为此,您应该使用 29 版本。

compileSdkVersion 29

And if you want to use Toolbar add this on your gradle file (Module:app):

如果您想使用工具栏,请将其添加到您的 gradle 文件 (Module:app) 中:

implementation 'com.google.android.material:material:1.1.0'

Use this Toolbar code in your XML file:

在您的 XML 文件中使用此工具栏代码:

<androidx.appcompat.widget.Toolbar />

@canerkaseler

@canerkaseler