【问题标题】:Error defining widget Toolbar in Android Studio在 Android Studio 中定义小部件工具栏时出错
【发布时间】:2014-12-25 11:38:37
【问题描述】:

我正在尝试使用新方法编写导航抽屉。当我试图定义我自己的应用工具栏时,我的问题就出现了。

这是我的 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"
    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 个类似错误)*

我该如何解决??

非常感谢。

【问题讨论】:

    标签: android toolbar


    【解决方案1】:

    我遇到了类似的错误,并通过更改我的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>
    

    还有我的工具栏小部件:

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

    我建议您将颜色自定义放在 styles.xml 而不是工具栏小部件中。

    【讨论】:

      【解决方案2】:

      相当老的问题。您必须使用操作栏主题作为

      <style name="AppBaseTheme" parent="Theme.AppCompat"> </style>
      

      attr 的正确语法是:

      ?android:attr/actionBarSize
      

      【讨论】:

        【解决方案3】:

        纠结了一段时间后,这些代码对我有用:

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

        可能只是缺少toolbarStyle字段,需要填写。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-30
          • 1970-01-01
          • 1970-01-01
          • 2016-01-18
          • 2013-09-03
          • 1970-01-01
          • 1970-01-01
          • 2015-02-28
          相关资源
          最近更新 更多