错误:“在Android Studion 3.1.3中无法在当前主题中找到样式'coordinatorLayoutStyle'”

时间:2018-07-06 18:12:02

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

我刚接触Android Studio,并一直尝试使用Navigation Drawer布局进行处理,但是我似乎无法解决错误找不到样式{{1} }设置为当前主题。我尝试在coordinatorLayoutStyle的App主题下添加<item name="coordinatorLayoutStyle">@style/Widget.Design.CoordinatorLayout</item>,并将styles.xml更改为implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'。这些都不适合我。以下是代码。

MainActivity.java

alpha1

styles.xml

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;
    }
}

build.gradle(Module:app)

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

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

4 个答案:

答案 0 :(得分:2)

我有同样的问题。问题是使用了compileSdkVersion。我将28替换为27。

此外,我将appcompat-v7:28.0.0-alpha1替换为appcompat-v7:27.1.1

这对我有用。

bool IEqualityComparer.Equals(object x, object y) {
  if (x is T && y is T)
    return this.Equals((T)x, (T)y); // known types, run Equals
  else
    return object.Equals(x, y);     // unknown type(s), run default Equals 
}

int IEqualityComparer.GetHashCode(object obj) {
  if (obj is T)
    return this.GetHashCode((T)obj);
  else
    return null == obj ? 0 : obj.GetHashCode();
}

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

尝试这样

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

答案 3 :(得分:0)

这项工作对我来说,我有同样的错误

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:我的英语说得不太好