【问题标题】:Error:'Failed to find style 'coordinatorLayoutStyle' in current theme' in Android Studion 3.1.3错误:Android Studio 3.1.3 中的“无法在当前主题中找到样式‘coordinatorLayout Style’”
【发布时间】:2018-07-06 18:12:02
【问题描述】:

我是使用 Android Studio 的新手,一直在尝试使用 Navigation Drawer 布局并对其进行处理,但是我似乎无法解决错误 Failed to find style coordinatorLayoutStyle in current theme 。我尝试在styles.xml 的App 主题下添加<item name="coordinatorLayoutStyle">@style/Widget.Design.CoordinatorLayout</item>,并将implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' 更改为alpha1。这些都不适合我。以下是代码。

MainActivity.java

package com.example.divya.niluv1;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="coordinatorLayoutStyle">@style/Widget.Design.CoordinatorLayout</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

build.gradle (Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.divya.niluv1"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta6'
    // implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: android android-layout android-studio android-constraintlayout


    【解决方案1】:

    我遇到了同样的问题。问题是使用的 compileSdkVersion。我把 28 换成了 27。

    另外,我用 appcompat-v7:27.1.1 替换了 appcompat-v7:28.0.0-alpha1

    这对我有用。

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.test.hello"
            minSdkVersion 16
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

    【讨论】:

      【解决方案2】:

      Lorenzo 的回答有效,但我个人发现它稍微改变了我的布局,但将以下内容添加到 values/styles.xml 对我有用

      <style name="AppTheme.NoActionBar">
      <item name="coordinatorLayoutStyle">@style/Widget.Design.CoordinatorLayout</item>
      </style>
      

      【讨论】:

        【解决方案3】:

        这样试试

        implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
        

        【讨论】:

          【解决方案4】:

          这对我有用,我遇到了同样的错误

          dependencies {
          implementation fileTree(dir: 'libs', include: ['*.jar'])
          implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
          implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
          implementation 'com.android.support.constraint:constraint-layout:1.1.2'
          testImplementation 'junit:junit:4.12'
          androidTestImplementation 'com.android.support.test:runner:1.0.2'
          androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
          implementation 'com.android.support:design:28.0.0-alpha1'}
          

          编辑: 在这条路线中:app>res>layout 你找到了 2 个 xml,被调用的 activity_main 在第二行有这个:

          android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
          

          最后是这个

          </android.support.design.widget.CoordinatorLayout>
          

          所以你需要做的是删除它们并粘贴它:

          <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          

          </android.support.constraint.ConstraintLayout>
          

          PD:你可以再次使用 alpha3 PD2:我也是新人,我不知道这会不会影响到其他事情 PD3:我的英语不太好

          【讨论】:

          • 我必须点击再试一次,我不知道为什么,但在那之后它可以工作。关于主题的问题是存在的,但不是一个真正的“问题”只是一点点ñe
          • 这确实有效。你是对的,它确实会出现渲染问题,但运行得很好。谢谢。
          猜你喜欢
          • 2018-11-26
          • 2016-12-19
          • 1970-01-01
          • 2018-08-15
          • 2011-10-21
          • 2018-11-24
          • 2012-05-25
          • 2012-01-20
          • 1970-01-01
          相关资源
          最近更新 更多