【问题标题】:Material design: Failed to find style 'toolbarStyle' in current theme材料设计:在当前主题中找不到样式“toolbarStyle”
【发布时间】:2017-04-02 19:49:29
【问题描述】:

我正在学习使用 Theme.Material.Light.NoActionBar 主题的材料设计。我正在尝试按照教程here 将工具栏添加到我的活动中。

style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

v21\style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorAccent</item>
        <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>
    </style>
</resources>

tool_bar.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="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:elevation="4dp">

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

build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    testCompile 'junit:junit:4.12'
}

但我收到了这个Rendering Problem

Missing styles. Is the correct theme chosen for this layout?  Use the Theme combo box above the layout to choose a different layout, or fix the theme style references.

Failed to find style 'toolbarStyle' in current theme

到目前为止我尝试过的所有方法都不起作用

  • 正在刷新布局。
  • 已将 android studio 更新到最新版本 2.2.2 并安装了在右上角弹出窗口中为我推荐的所有内容。
  • 点击File&gt;Invalidate caches/restart
  • 将预览窗口中的API version in editor 更改为2122232425 - 我无法在低于 21 的版本上尝试预览(材料设计要求/还是我错了???)-.
  • 将预览窗口中的Theme in editor 更改为NoActionBar 材质主题和AppTheme - 将其更改为AppCompat 主题没有用,因为我将无法查看卡片等材质元素等等-。
  • 我也试过改成AppCompat,但是当我从主题列表中选择它并按确定时,它并没有成为选定的; AppTheme 成为预览的选定主题。

我也尝试添加&lt;item name="toolbarStyle"&gt;@style/Widget.AppCompat.Toolbar&lt;/item&gt;,但它给了我另一个警告:Failed to find '?attr/actionBarSize' in current theme

预览中的问题可能是什么,我该如何解决。
另一个问题,据我所知,AppCompat 主题不包含Material 主题的所有功能。为什么其他人会use it to solve the problem of preview 甚至add a reference to it in the v21\style.xml file 而不是引用Material 主题中存在的Toolbar

【问题讨论】:

    标签: android material-design android-toolbar android-theme android-styles


    【解决方案1】:

    试试这个

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
    
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
    

    【讨论】:

    • 在构建项目后显示了很多错误。 Image here.
    【解决方案2】:

    您有两个或两个同名的样式主题。

    您必须将其中一个更改为不同的名称,例如 name="CustomTheme" 或 "myTheme" 或任何您想要的名称。

    另一方面,当您从这些教程中学习时,请务必检查您的 Android Studio 版本。事情正在快速更新,因此请在 android Dev 页面或任何其他页面上了解发生了什么变化。

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2016-12-19
      • 2018-12-05
      • 2018-08-23
      • 2011-11-14
      • 1970-01-01
      • 2018-08-15
      • 2012-01-20
      相关资源
      最近更新 更多