在Android Studio中定义小部件工具栏时出错
我正在尝试用新的方法编写一个导航抽屉。我现在的问题是,当我试图定义自己的应用程序工具栏时 这是我的XML代码在Android Studio中定义小部件工具栏时出错,android,toolbar,Android,Toolbar,我正在尝试用新的方法编写一个导航抽屉。我现在的问题是,当我试图定义自己的应用程序工具栏时 这是我的XML代码 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="@dimen/app_bar_top_padding"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="@dimen/app_bar_top_padding"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
app:theme="@style/MyCustomToolBarTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
这是app_bar.xml
在android studio的预览中,当尝试渲染时,出现以下错误:
*缺少样式。是否为此布局选择了正确的主题?使用版面上方的主题组合框选择其他版面,或修复主题样式引用。-在当前主题中找不到样式“toolbarStyle”(未显示4个类似错误) -属性“minHeight”中的“?attr/actionBarSize”格式无效。(编辑)(未显示2个类似错误)* 我怎样才能解决它
非常感谢。我遇到了类似的错误,通过更改
style.xml
文件中的parent
属性解决了这个问题。解决方案是使用与应用程序主题相同的父主题,如下所示:
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="Widget.MyApp.ActionBar" parent="AppTheme">
<item name="android:background">@color/title_color</item>
</style>
</resources>
@颜色/标题\u颜色
和我的工具栏小部件:
<android.support.v7.widget.Toolbar
android:id="@+id/title_bar"
android:gravity="top|center"
style="@style/Widget.MyApp.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
我建议您将颜色自定义设置放在
style.xml
中,而不是工具栏小部件中。这是一个很老的问题。必须将动作栏主题用作
<style name="AppBaseTheme" parent="Theme.AppCompat"> </style>
经过一段时间的挣扎,这些代码对我来说很有用:
<resources>
<!-- Base application theme. -->
<style name="MyCustomToolbarTheme" parent="android:Theme.Material.Light>
<item name="toolbarStyle">@style/ToolbarStyle</item>
</style>
<style name="ToolbarStyle" parent="Theme.AppCompat">
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="android:actionMenuTextColor">@android:color/white</item>
</style>
</resources>
<resources>
<!-- Base application theme. -->
<style name="MyCustomToolbarTheme" parent="android:Theme.Material.Light>
<item name="toolbarStyle">@style/ToolbarStyle</item>
</style>
<style name="ToolbarStyle" parent="Theme.AppCompat">
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="android:actionMenuTextColor">@android:color/white</item>
</style>
</resources>