2014-02-08 129 views
2

我想我的Android對話框看起來是這樣的:如何解決我的對話框暗淡和alpha效果

enter image description here

但使用此XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:alpha="0.20" 
    android:backgroundDimAmount="0.0"> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" /> 

    <Button 
     android:id="@+id/socialBtn1" 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:scaleType="fitXY" 
     android:src="@android:color/tab_indicator_text" 
     android:text="@string/fb_share" /> 

導致該對話框:

enter image description here

問題:

(1)的按鈕應具有的α= 1,但似乎與阿爾法0.2

(2)的對話框BG應具有的α= 0.2,但是用α1

(3)的bG似乎周圍應與暗淡= 0

回答

0

您將不得不設置背景圖像或顏色以獲得所需的效果。

例如,如果使用顏色,則對話框的半透明背景可以通過在Linearlayout屬性中添加下面一行來實現。

android:background="#B2000000" 

前2位決定透明效果。 格式爲#AARRGGBB,其中AA爲alpha通道,RR爲紅色通道,GG爲綠色通道,BB爲藍色通道。用255計算AA不透明度百分比。例如對於30%透明度,即70%不透明度:255 * 0.7 = 178,以十六進制B2表示。所以對於黑色半透明背景顏色來說#B2000000

類似於有固定的按鈕顏色設置顏色或您的自定義背景圖像。

android:background="@drawable/ic_button_bg" 
+0

非透光bg如何?我想讓周圍的環境變得清晰 –

+0

@EladBenda:我假設圍繞着你,仍然是指對話框內的區域,即圍繞着按鈕。如果是,則在上例中刪除半透明從#AARRGGBB中刪除AA,因此對於黑色不透明背景,它將變爲android:background =「#000000」。 – Mili

3

(1)的按鈕應具有的α= 1,但用α-0.2

即默認按鈕的背景看起來。你必須給出使用按鈕的屬性android:background特定的背景。我不認爲android:src="@android:color/tab_indicator_text"適用於按鈕。該屬性用於ImageView

(2)對話框BG應有的α= 0.2,但與阿爾法似乎1

確定alpha值爲0.2對話框的背景?通過查看設計,我估計alpha值應該是0.7 - 0.8,其結果是部分透明度。並且您只設置alpha。您還需要指定背景顏色。我建議使用dialog.getWindow().setBackgroundDrawable方法動態給出alpha值。

例子:

final Dialog dialog = new Dialog(MainActivity.this); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.dialog); 
Drawable d = new ColorDrawable(Color.BLACK); 
d.setAlpha(178); //alpha 0.7 (0-250 range) 
dialog.getWindow().setBackgroundDrawable(d); 
dialog.show(); 

對話框佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="500dp" 
    android:backgroundDimAmount="0.0" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/socialBtn1" 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:scaleType="fitXY" 
     android:background="#0066FF" 
     android:layout_marginTop="10dp" 
     android:text=" Facebook " /> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" /> 

</LinearLayout> 

結果在下面的對話框

enter image description here

+0

我不明白:我怎樣才能使對話框背景部分透明?現在唯一的按鈕變得部分透明,這些是我想保持全透明的唯一的按鈕 –

+0

正如我所說的動態使用'getWindow()。setBackgroundDrawable'方法給alpha,使用'android給出'Button'的特定顏色/ drawable :背景屬性。 –

0

你應該嘗試

Dialog mdialog =new Dialog(this); 
Drawable d = new ColorDrawable(Color.BLACK); 
d.setAlpha(55); 
mdialog.getWindow().setBackgroundDrawable(d);