工具栏与渐变背景设置标题背景透明

时间:2015-03-02 18:42:28

标签: android

我定义了Gradient Drawable:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:endColor="#2F3F59"
        android:startColor="#4078A7"
        android:type="linear"
        android:angle="90" />

</shape>

我将其设置在我的工具栏上:

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/textcolorsecundary</item>
    <item name="actionMenuTextColor">@color/textcolorsecundary</item>
    <item name="android:textColorSecondary">@color/textcolorsecundary</item>
    <item name="android:background">@drawable/custom_background_blue</item>
</style>

这很有效!的 BUT

enter image description here

这就是标题所发生的情况。它获得相同的渐变。这看起来很难看,所以我必须改变它。如何将信息文本的背景设置为透明?

4 个答案:

答案 0 :(得分:3)

您也可以通过将工具栏的背景设置为可绘制来解决此问题。

mToolbar().setDrawable(R.drawable.your_gradient)

答案 1 :(得分:0)

link - Set transparent background of an imageview on Android

在您的颜色xml文件中使用此颜色,然后将此颜色赋予您的起始颜色和结束颜色

答案 2 :(得分:0)

解决方法是禁用工具栏的标题:

this.getSupportActionBar().setDisplayShowTitleEnabled(false);

并添加自定义TextView

<?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/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:elevation="2dp"
    android:focusable="false"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:theme="@style/AppTheme.Toolbar" >

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="@null"
        android:text="Toolbar Title"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

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

答案 3 :(得分:0)

在样式部分中将背景颜色更改为透明应解决此问题

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">#00000000</item>
</style>

问题How to make title background transparent on android app在解决方案上方也有类似的问题。安卓应用适用于瑞萨电子RX130和德州仪器CC2650 BLE解决方案。波纹管是解决方案之后的解决方案。

enter image description here