AppCompat风格背景的工具栏中传播到图像 [英] AppCompat style background propagated to the Image within the ToolBar

查看:191
本文介绍了AppCompat风格背景的工具栏中传播到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

我使用的是最新的AppCompat V7 LIB(21.0.0),我已经迁移的动作条我的应用程序工具栏

问题

  1. 在工具栏的图像自动接收相同背景的工具栏

目前搜索框是一个自定义视图的操作栏设置(从previous实现)我打算如果切换到搜索查看,并相应的风格,但我仍然在我朝右这个问题很感兴趣现在。

<醇开始=2>
  • 在当前在工具栏菜单项长pressing干杯出现的提示文本和文本具有相同的背景比工具栏。
  • 我怎样才能避免这种情况?

    下面是我的工具栏布局和样式和放大器;主题

    布局v_toolbar.xml

     &LT; XML版本=1.0编码=UTF-8&GT?;
    &LT; android.support.v7.widget.Toolbar
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        机器人:ID =@ + ID / my_awesome_toolbar
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
        应用程序:主题=@风格/ MyActionBarStyle/&GT;
     

    值/ styles.xml

     &LT;样式名称=MyActionBarStyle父=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar&GT;
        &LT;项目名称=机器人:背景&GT; @色/绿色&LT; /项目&GT;
        &LT;项目名称=机器人:windowActionBarOverlay&GT;真&LT; /项目&GT;
    
        &LT;! - 支持库的兼容性 - &GT;
        &LT;项目名称=背景&GT; @色/绿色&LT; /项目&GT;
        &LT;项目名称=windowActionBarOverlay&GT;真&LT; /项目&GT;
    &LT; /风格&GT;
     

    主题

     &LT;样式名称=MyTheme的父=AppBaseTheme&GT;
        &LT;! - 在这里,我们设置appcompat的actionBarStyle  - &GT;
        &LT;! - &LT;项目名称=actionBarStyle&GT; @风格/ MyActionBarStyle&LT; /项目&GT; - &GT;
    
        &LT;项目名称=windowActionBar&GT;假&LT; /项目&GT;
        &LT;  - !...在这里,我们设置appcompat的颜色主题化ATTRS  - &GT;
        &LT;项目名称=colorPrimary&GT; @色/绿色&LT; /项目&GT;
        &LT;项目名称=colorPrimaryDark&GT; @色/ green_dark&LT; /项目&GT;
    &LT; /风格&GT;
     

    <打击> 解决方法1问题1

    解决方法对于第一个问题是设置在ImageView的透明背景

     安卓背景=@色/透明
     

    这并不能解决敬酒问题。

    解决方案这两个问题

    我设置背景到工具栏

     &LT; XML版本=1.0编码=UTF-8&GT?;
    &LT; android.support.v7.widget.Toolbar
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        机器人:ID =@ + ID / my_awesome_toolbar
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@色/绿
        应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
        应用程序:主题=@风格/ MyActionBarStyle/&GT;
     

    和我没有设定的主题背景了。

     &LT;样式名称=MyActionBarStyle父=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar&GT;
        &LT;项目名称=机器人:windowActionBarOverlay&GT;真&LT; /项目&GT;
        &LT;! - 支持库的兼容性 - &GT;
        &LT;项目名称=windowActionBarOverlay&GT;真&LT; /项目&GT;
    &LT; /风格&GT;
     

    正如克里斯·巴内斯,一种风格可以为工具栏创建宣布了Android,而不是:背景

    这解决了我的问题,但我似乎有另外一个问题是,popupTheme不适用,我的ShareActionMenuItem下拉没有一个白色背景黑色文本的事实。 在这里看到另一个问题: AppCompat工具栏popupTheme在ShareAction菜单项不使用

    这也许是在appcompat库中的缺陷。

    解决方案

    您不应该使用主题这样的:

    • 在风格=本地到工具栏
    • 主题=全局膨胀的一切在工具栏

    解决方法2,您拨打的,是一种正确的做法。如果你想提取一些值的样式,你可以做到这一点像这样:

     &LT; android.support.v7.widget.Toolbar
        机器人:ID =@ + ID / my_awesome_toolbar
        机器人:layout_width =match_parent
        机器人:layout_height =?ATTR / actionBarSize
        风格=@风格/ MyDarkToolbar/&GT;
     

    风格:

     &LT;样式名称=MyDarkToolbarStyle父=Widget.AppCompat.Toolbar&GT;
        &LT;项目名称=机器人:背景&GT; @色/绿色&LT; /项目&GT;
        &LT;项目名称=popupTheme&GT; @风格/ ThemeOverlay.AppCompat.Light&LT; /项目&GT;
        &LT;项目名称=主题&GT; @风格/ ThemeOverlay.AppCompat.Dark.ActionBar&LT; /项目&GT;
    &LT; /风格&GT;
     

    Context

    I'm using the newest AppCompat v7 lib (21.0.0) and I have migrated my app from ActionBar to ToolBar

    Problem

    1. Images in the ToolBar automatically receive the same background as the ToolBar

    Currently the SearchBox is a custom view set on the action bar (From previous implementation) I plan to switch if to the SearchView and style it accordingly but I'm still very interested in the problem I'm facing right now.

    1. When long pressing on a menu item in the ToolBar a toast appear with the hint text and the text has the same background than the ToolBar.

    How can I avoid this?

    Here is my toolbar layout and the style & theme

    layout v_toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/MyActionBarStyle"/>
    

    values/styles.xml

    <style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:background">@color/green</item>
        <item name="android:windowActionBarOverlay">true</item>
    
        <!-- Support library compatibility -->
        <item name="background">@color/green</item>
        <item name="windowActionBarOverlay">true</item>
    </style>
    

    Theme

    <style name="MyTheme" parent="AppBaseTheme">
        <!-- Here we setting appcompat’s actionBarStyle -->
        <!-- <item name="actionBarStyle">@style/MyActionBarStyle</item> -->
    
        <item name="windowActionBar">false</item>
        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryDark">@color/green_dark</item>
    </style>
    

    Workaround 1 for problem 1

    The workaround for the first problem is to set a transparent background on the ImageView

    android:background="@color/transparent"
    

    This does not solve the toast issue.

    Solution for both problems

    I set a background to the ToolBar

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/MyActionBarStyle"/>
    

    And I'm not setting the background on the theme anymore.

    <style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:windowActionBarOverlay">true</item>
        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>
    </style>
    

    As pointed by Chris Banes, a style could be created for the toolbar instead of declaring the android:background.

    This fixes my problem but then I seem to have another issue with the fact that the popupTheme isn't applied and my ShareActionMenuItem drop down does not have a white background with black text. See another question here: AppCompat ToolBar popupTheme not used in the ShareAction MenuItem

    Maybe this is a bug in the appcompat library.

    解决方案

    You should not be using theme like that:

    • style = local to the Toolbar
    • theme = global to everything inflated in the Toolbar

    Workaround 2, as you call it, is kind of the correct way. If you want to extract some values into a style, you can do it as so:

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        style="@style/MyDarkToolbar" />
    

    Style:

    <style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
        <item name="android:background">@color/green</item>
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    </style>
    

    这篇关于AppCompat风格背景的工具栏中传播到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆