2015-03-02 138 views
4

我定義了一個漸變繪製對象:工具欄與漸變背景設置標題背景透明

<?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> 

這是工作!

enter image description here

這是標題會發生什麼。它獲得相同的梯度。這看起來真的很醜,所以我必須改變這一點。我如何設置該信息文本的背景是透明的?

+0

檢查這一點 - http://stackoverflow.com/questions/1492554/set-transparent-background-of-an-imageview-in-android – 2015-03-02 18:47:05

+0

你好。我不認爲這可以幫助我。我懷疑標題是一個ImageView,我也不知道如何訪問工具欄標題的背景。 – Mulgard 2015-03-02 18:49:57

回答

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> 
0

鏈接 - Set transparent background of an imageview on Android

使用這種顏色在你的顏色的XML文件,然後把這個顏色你startcolor和結束顏色

+0

這是完全錯誤的。我不希望我的工具欄是透明的....我希望標題的背景是透明的...... – Mulgard 2015-03-03 07:33:17

+0

我說,只是創建翻譯顏色,您可以使用任何地方 – 2015-03-03 14:15:25

+0

以及如何訪問背景工具欄的標題?那是核心問題。設置透明背景不是問題! – Mulgard 2015-03-03 14:51:35

2

您也可以通過將工具欄的背景設置爲drawable來解決此問題。

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