2011-09-13 46 views
12

在我的應用程序中,我已經設置了頂層linearlayout的背景圖像,然後淡入背景,我將其alpha設置爲.2但這會產生一個奇怪的問題,它也爲佈局的所有子元素設置了alpha,即使我明確定義了子元素中的一個不同的alpha值。爲什麼子視圖繼承父佈局的alpha值

是否可以設置家長的alpha值而不影響孩子的alpha值?

有什麼方法可以在頂級視圖上設置alpha而不影響該視圖children上的alpha?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ll1" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:background="@drawable/bg" 
    android:alpha="0.2"> 
     <Button android:id="@+id/btn1" 
      android:text="Set 1" 
      android:layout_width="300px" 
      android:layout_height="150px" 
      android:layout_gravity="center" 
      android:background="@drawable/button1" 
      android:tag="1" 
      android:alpha="1"/> 
     <Button android:id="@+id/btn2" 
      android:text="Set 2" 
      android:layout_width="300px" 
      android:layout_height="150px" 
      android:layout_gravity="center" 
      android:background="@drawable/button2" 
      android:tag="2"/> 
</LinearLayout> 

回答

20

這正是它打算如何工作。

爲什麼不簡單地將可繪製的背景「alpha drawable/bg」更改爲0.2?

或者,嘗試的FrameLayout與此基本結構:

<FrameLayout> 
    <ImageView 
     android:background="@drawable/bg" 
     android:alpha="0.2" /> 
    <LinearLayout> 
     <Button /> 
     <Button /> 
    </LinearLayout>  
</FrameLayout> 
+0

非常好的主意! =) –

+0

不錯的IDE,它爲我工作... – krisDrOid

+0

感謝我的工作 –

1

您將需要使用framelayout。其他明智的你的按鈕是由線性佈局enlosed與aplha設置,這也會影響按鈕。

3

的一種方法是設置哈希碼內的α值爲顏色即代替#RRGGBB使用#AARRGGBB。這確保了alpha值不會被繼承。