【发布时间】: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>Invalidate caches/restart。 - 将预览窗口中的
API version in editor更改为21、22、23、24和25- 我无法在低于 21 的版本上尝试预览(材料设计要求/还是我错了???)-. - 将预览窗口中的
Theme in editor更改为NoActionBar材质主题和AppTheme- 将其更改为AppCompat主题没有用,因为我将无法查看卡片等材质元素等等-。 - 我也试过改成
AppCompat,但是当我从主题列表中选择它并按确定时,它并没有成为选定的;AppTheme成为预览的选定主题。
我也尝试添加<item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>,但它给了我另一个警告: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