工具栏背景正在使用mipmap而不是drawable

时间:2017-04-29 18:16:59

标签: java android toolbar

我在工具栏中使用了背景图片,并且由于某种原因,工具栏只能使用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:错误膨胀   class android.support.v7.widget.Toolbar

必须提到这个mipmap使用只是让我的应用程序非常慢,并且它跳过帧,所以我想将它改为drawable。

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

编辑:

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>

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

3 个答案:

答案 0 :(得分:1)

似乎你的xml文件很完美。 我的建议是,只需调整图像大小或准备不同大小的背景图像,然后将其设置到工具栏中并将其放入不同的drawables / mipmap文件夹中。

  

机器人:背景= “@ mip映射/栏”

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

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

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

答案 1 :(得分:0)

请在您的应用级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"/>

<强> ToolBarStyle

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

我希望这会对你有所帮助

答案 2 :(得分:0)

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

尝试这种方法,它有效..