【问题标题】:Toolbar background is working with mipmap and not drawable工具栏背景正在使用 mipmap 并且不可绘制
【发布时间】:2017-04-29 18:16:59
【问题描述】:

我在我的工具栏中使用了背景图片,由于某种原因,工具栏只适用于 mipmap 而不是 drawable。

在我的 app_bar_main.xml 文件中:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@mipmap/bar"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

如您所见,我使用 mipmap 而不是 drawable,但是当我使用 drawable 时应用程序崩溃并给出以下错误:

二进制 XML 文件第 11 行:二进制 XML 文件第 16 行:膨胀错误 类 android.support.v7.widget.Toolbar

必须提到这个mipmap使用只是让我的应用程序非常慢,而且它会跳帧,所以我想把它改成drawable。

如何将图像的工具栏背景资源从mipmap更改为drawable?

编辑:

样式.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>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>


    <style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="background">@drawable/bar</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

【问题讨论】:

  • 你的suppport library to 23.2.1 是什么?
  • @Ninja 我在哪里可以查看支持?我的构建 gradle 文件说:'com.android.support:support-v4:25.0.0'

标签: java android toolbar


【解决方案1】:

看来你的 xml 文件是完美的。 我的建议是,只需调整该图像的大小或准备不同大小的背景图像,您希望将其设置到工具栏并将其放入不同的绘图/mipmap 文件夹中。

android:background="@mipmap/bar"

我们知道图标大小应该是,

  • drawable-ldpi(120 dpi,低密度屏幕)- 36px x 36px
  • drawable-mdpi(160 dpi,中等密度屏幕)- 48px x 48px
  • drawable-hdpi(240 dpi,高密度屏幕)- 72px x 72px
  • drawable-xhdpi(320 dpi,超高密度屏幕)- 96px x 96px

再次运行您的应用并告诉我。

【讨论】:

    【解决方案2】:

    工具栏.xml

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/MyToolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
    

    主题/风格

    <style name="MyToolbarStyle">
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
    <item name="android:background">@color/primary</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
    <!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
         this should go to the AppTheme -->
    

    试试这个方法,效果不错..

    【讨论】:

    • 想知道这与我的代码有什么关系,这里不包括图像背景。
    • 我不确定,但是如果您需要将背景组设置为图像,则需要使用 mipmap
    • 虽然 mipmap 让我的应用运行缓慢(非常慢)
    • 请参考这个链接stackoverflow.com/questions/32607496/… ...看第一个答案你可以试试
    【解决方案3】:

    请将此代码添加到您的应用级别build.gradle 文件中

    android {  
       defaultConfig {
         .....
         vectorDrawables.useSupportLibrary = true
        }  
     }
    

    toolbar_default.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        style="@style/ToolBarStyle"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material"/>
    

    工具栏样式

    <style name="ToolBarStyle" parent="">
            <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
            <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
     </style>
    

    其他 请从您的工具栏中删除以下行并尝试检查它是否有效。我不确定,但您只是检查它。

    app:popupTheme="@style/AppTheme.PopupOverlay"
    

    希望对你有帮助

    【讨论】:

    • Binary XML file line #11: Binary XML file line #16: Error inflating class android.support.v7.widget.Toolbar
    • toolbar的风格是什么?
    • 我想要工具栏的图像背景,这对我没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多