试图使工具栏内的文本背景透明

时间:2018-08-17 15:45:24

标签: android android-layout android-toolbar

我有一个工具栏,其中包含如下所示的标题:

enter image description here

如何消除标题文本“相机”后面的背景颜色?

这是我的布局:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_view_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="?attr/actionBarSize"
        tools:context=".ActivityGallery">
    </android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar_top"
        android:theme="@style/StyleToolBarWhite" />
</FrameLayout>

还有我的styles.xml:

<style name="StyleToolBarWhite">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="actionMenuTextColor">@android:color/white</item>
    <item name="titleTextAppearance">@style/StyleToolbarText</item>

    <item name="android:background">@color/colorTint</item>
    <!-- Support library compatibility -->
    <item name="background">@color/colorTint</item>
</style>

<style name="StyleToolbarText">
    <item name="android:background">@null</item>
</style>

@color/colorTint是整个工具栏的透明颜色。如果我删除样式中的background项目,除工具栏外,我将得到所需的颜色,然后将失去“色调”颜色并变得完全透明。如何将整个工具栏的背景颜色与工具栏内文本的背景分开?

1 个答案:

答案 0 :(得分:2)

<style name="StyleToolBarWhite">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="actionMenuTextColor">@android:color/white</item>
</style>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorTint"
    android:theme="@style/StyleToolBarWhite" />