2011-05-12 155 views
182

我無法將漸變背景應用於LinearLayout。Android LinearLayout漸變背景

這應該是相對簡單的,因爲我已閱讀,但它似乎並沒有工作。有關參考資料,我正在開發2.1-update1。

header_bg.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:angle="90" 
     android:startColor="#FFFF0000" 
     android:endColor="#FF00FF00" 
     android:type="linear"/> 
</shape> 

main_header.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:orientation="horizontal" 
    android:background="@drawable/header_bg"> 
</LinearLayout> 

如果我改變@抽拉/ header_bg到彩色 - 例如#FF0000它工作得很好。我在這裏錯過了很明顯的東西嗎

+0

機器人:機器人backgroundTint:backgroundTintMode http://stackoverflow.com/a/43341289/3209132 – 2017-04-28 07:44:27

回答

329

好吧我已經設法解決這個使用選擇器。請參見下面的代碼:

main_header.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:orientation="horizontal" 
    android:background="@drawable/main_header_selector"> 
</LinearLayout> 

main_header_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape> 
     <gradient 
      android:angle="90" 
      android:startColor="#FFFF0000" 
      android:endColor="#FF00FF00" 
      android:type="linear" /> 
    </shape> 
</item> 
</selector> 

希望這有助於誰的人有同樣的問題。

+2

太好了。僅供參考,請參閱其他漸變類型:http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#attr_android:type – Bamaco 2016-04-07 21:36:46

15

嘗試刪除android:gradientRadius =「90」。這是一個適用於我的:

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
> 
    <gradient 
     android:startColor="@color/purple" 
     android:endColor="@color/pink" 
     android:angle="270" /> 
</shape> 
+0

不幸的是,這不適合我。我已經用我現在的東西更新了原始問題。 – Genesis 2011-05-12 11:07:01

+0

在佈局中添加小部件時是否仍然無法工作(例如,TextView? – 2011-05-12 11:32:17

+0

正確 - 它仍然不能在佈局中使用TextView。如果我應用靜態顏色而不是繪製,它只能工作好的。我注意到的一件事是我可以(有時)使用選擇器來工作,但根據我的理解,這應該不是必需的。 – Genesis 2011-05-12 13:54:01

0

我會檢查你的alpha通道你的漸變顏色。對我來說,當我測試我的代碼時,我的alpha通道在顏色上設置錯誤,並且對我沒有任何作用。一旦我得到了alpha通道設置它一切工作!

5

我的問題是.xml擴展名未添加到新創建的XML文件的文件名中。添加.xml擴展名解決了我的問題。

28

也有可能有第三種顏色(中心)。和不同種類的形狀。

例如,在繪製/ gradient.xml:

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#000000" 
     android:centerColor="#5b5b5b" 
     android:endColor="#000000" 
     android:angle="0" /> 
</shape> 

這給你黑 - 灰 - 黑(左到右),這是我最喜歡的深色背景大氣壓。

請記住添加漸變。XML作爲在佈局XML背景:

android:background="@drawable/gradient" 

也可以旋轉,用:

角= 「0」

爲您提供了一個垂直線

並與

angle =「 90"

給你一個水平線

可能的角度是:

0,90,180,270

而且很少有不同種類的形狀:

android:shape =「rectangle」

圓角形狀:

機器人:形狀= 「橢圓形」

和problably幾個。

希望它有幫助,歡呼!

1

我不知道這是否會幫助任何人,但我的問題是,我試圖梯度設置,像這樣的的ImageView的「源」屬性:

<ImageView 
    android:id="@+id/imgToast" 
    android:layout_width="wrap_content" 
    android:layout_height="60dp" 
    android:src="@drawable/toast_bg" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY"/> 

不是100%確定爲什麼沒有工作,但現在我改變了它,並把繪製在ImageView的母公司,這是一個RelativeLayout的在我的情況下,像這樣的「背景」屬性:(這種成功合作)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/custom_toast_layout_id" 
    android:layout_height="match_parent" 
    android:background="@drawable/toast_bg"> 
10

在XML可繪製文件:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <gradient android:angle="90" 
       android:endColor="#9b0493" 
       android:startColor="#38068f" 
       android:type="linear" /> 
     </shape> 
    </item> 
</selector> 

在您的佈局文件:機器人:背景= 「@繪製/ gradient_background」

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/gradient_background" 
     android:orientation="vertical" 
     android:padding="20dp"> 
     ..... 

</LinearLayout> 

enter image description here