2015-05-29 32 views
2

當我嘗試將背景設置爲透明時,我的工具欄始終保持灰色。 這是我的XML。無法在Android中使工具欄透明

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:minHeight="@dimen/abc_action_bar_default_height_material" 
    app:theme="@style/Rushmore.Toolbar.Transparent" /> 

我的主題

<style name="Rushmore.Toolbar.Transparent" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
     <item name="android:windowActionBarOverlay">true</item> 

     <!-- Support Library compability --> 
     <item name="windowActionBarOverlay">true</item> 
    </style> 

我從代碼

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); 
     toolbar.setBackgroundResource(Color.TRANSPARENT); 

嘗試了我不知道它是什麼,我缺少的...

+0

http://developer.android.com/training/basics/actionbar/overlaying.html看看這個鏈接。這給出瞭如何覆蓋工具欄 – arjun

+0

@arjun是否爲操作欄?我已經設置了 true如上所述 –

+0

嘗試更改自定義樣式的父級,並將樣式設置爲活動而不是工具欄。疊加應該在活動層面完成。 – arjun

回答

7

我在試圖找到解決這個問題的最糟糕的麻煩。我一定找過幾十個帖子,空手而歸。最後,我碰巧發表了一篇文章(我忘記了哪個頁面),提到Android v7工具欄需要位於佈局的頂部以便透明化工作。謝天謝地,它工作。所以,我希望這會幫助別人:

這裏有您需要什麼:

  1. 定義佈局爲您的活動像下面
  2. 確保工具欄是在XML的底部,所以它會在z順序上位於最前面。
  3. 使用RelativeLayout,因此您可以確保工具欄在屏幕左上角可視化。
  4. @ color/primary應該是透明的(#00000000)或者如果你還需要使用其他顏色,你可以在代碼中設置它。
  5. (可選的)要麼添加「機器人:layout_marginTop =」?機器人:ATTR/actionBarSize」下面OR它添加在你的代碼的容器,否則,一些在FrameLayout裏的內容將是操作欄下方。

    <android.support.v4.widget.DrawerLayout 
        android:id="@+id/drawer_layout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
        <FrameLayout 
         android:id="@+id/container" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         /> 
    
        <ListView android:id="@+id/left_drawer" 
         android:layout_width="260dp" 
         android:layout_height="match_parent" 
         android:layout_gravity="start" 
         android:layout_marginRight="8dp" 
         android:layout_marginTop="?android:attr/actionBarSize" 
         android:choiceMode="singleChoice" 
         android:divider="@android:color/transparent" 
         android:dividerHeight="0dp" 
         android:background="@color/drawer_background" 
         /> 
    
    </android.support.v4.widget.DrawerLayout> 
    
    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/actionBarSize" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:background="@color/primary" 
        /> 
    

+0

你如何能夠使用多個根標籤?我收到錯誤:「多根標籤」。我把它放在DrawerLayout裏面,它工作。但有一個問題 - 工具欄的漢堡包菜單圖標已就位,但工具欄標題已與活動之間對齊,並與其他視圖重疊。 –

+0

我沒有。你需要把它放在另一個佈局中。 – MCLLC