Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

android - AppCompat style background propagated to the Image within the ToolBar

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.

Img background

  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.

Toast background

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...