Text background issue on Toolbar

三世轮回 提交于 2020-06-26 03:50:16

问题


When adding some alpha to my Toolbar background color, I notice there is a background applied to the title's TextView:

Here is my layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:background="#0099cc">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:theme="@style/FullscreenActionBarStyle"
        app:titleTextAppearance="@style/ActionBarTitle"/>

    <ImageView
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image"/>

</FrameLayout>

And my styles.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>

        <item name="android:windowActionBar">false</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="FullscreenTheme" parent="AppTheme">
        <item name="android:windowBackground">@null</item>
        <item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
        <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>

        <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
        <item name="android:windowActionBarOverlay">true</item>
        <!--For compatibility-->
        <item name="actionBarStyle">@style/FullscreenActionBarStyle</item>
        <item name="windowActionBarOverlay">true</item>
    </style>

    <style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
        <item name="background">@color/black_overlay</item>
        <item name="android:background">@color/black_overlay</item>
    </style>

    <style name="ActionBarTitle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">50sp</item>
    </style>
</resources>

"black_overlay" color value is #66000000

I am using AppCompat and Support libraries v24.2.1.

Do you have any idea on how to get rid of this text background?

Cheers.


回答1:


As you can see in the following issue, the problem comes from the attribute app:theme. It is applied to every view that a viewgroup inflate. Replace app:theme by style:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/FullscreenActionBarStyle"
    app:titleTextAppearance="@style/ActionBarTitle"/>



回答2:


Alright, removing the background properties on the FullscreenActionBarStyle style and putting it in the Toolbar layout definition make it work. As @apelsoczi pointed out, settings these properties in the toolbar style also apply them on children components.




回答3:


Encapsulate a TextView inside the Toolbar and set the value of that.

<android.support.v7.widget.Toolbar
        ...>
        <TextView ... />
</android.support.v7.widget.Toolbar>


来源:https://stackoverflow.com/questions/39639415/text-background-issue-on-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!