2013-02-21 24 views
27

我正在構建自定義DialogFragment。對話框佈局設置爲my_dialog.xml,但我怎樣才能修改對話框的顏色(透明灰色)?如何更改DialogFragment周圍的背景顏色?

enter image description here

my_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" > 

    <TextView 
     android:id="@+id/hello" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@android:color/holo_orange_light" 
     android:gravity="center" 
     android:text="hello" /> 

</RelativeLayout> 

MyDialogFragment.java

public class MyDialogFragment extends DialogFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.my_dialog, container); 

     getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

     return view; 
    } 
} 

回答

37

我必須設置android:windowIsFloating爲false,並android:windowBackground到我的自定義顏色的對話風格:

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <style name="MyDialog" parent="@android:style/Theme.Dialog"> 
     <item name="android:windowFrame">@null</item> 
     <item name="android:windowBackground">@color/orange_transparent</item> 
     <item name="android:windowIsFloating">false</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="android:windowTitleStyle">@null</item> 
     <item name="android:colorBackgroundCacheHint">@null</item> 
     <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
     <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
     <item name="android:gravity">center</item> 
    </style> 

</resources> 

MyDialogFragment

public class MyDialogFragment extends DialogFragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog); 
    } 
} 
+0

它的工作原理,非常感謝。這個帶有樣式行爲的對話框雖然很奇怪,不清楚。 – 2015-07-08 08:01:42

+2

請注意,如果將'windowBackground'更改爲某種顏色,則會失去使用圓角的默認DialogFragment窗口樣式。爲了保持這一點,你必須創建一個矩形的可繪製顏色作爲背景(即固體參數),並使用帶有半徑值的拐角。 – 2016-08-05 12:31:31

+0

如何在Dialog類中做到這一點?我的意思是我擴展對話框,而不是DialogFragment,並沒有setStyle()... – sasan 2017-06-28 08:21:55

0

改變你對XML 文本Background我只是編輯您的代碼替換它的機智^ h你

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" > 

<TextView 
    android:id="@+id/hello" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:gravity="center" 
    android:text="hello" /> 

,因爲你給了TextView高度和100dp的寬度。併爲它設置一個填充整個對話框的背景。因爲你的主要佈局是wrap_content。 請接受答案,如果這有助於你。
編輯:改變background 只需添加到您的佈局,或者你的TextView android:background="#232323"你可以改變這些數字給你喜歡的任何顏色。或者您可以設置從背景drawableandroid:background="@drawable/yourpicturename"

+0

我已經設置背景的背景我的TextView。我的問題是* TextView左右的顏色。 – jul 2013-02-21 17:28:54

+0

顏色是由設置您的textView中的背景,因爲這個android:background =「@ android:color/holo_orange_light」 我已經從你的佈局中刪除了這個。你能複製並粘貼我的上面的代碼,看看它將如何工作! – k0sh 2013-02-21 17:35:59

+0

是的,我知道,我想這樣。我想修改它周圍的灰色。 – jul 2013-02-21 18:32:48

15

,我發現我只需要做到這一點:

<style name="MyDialog" parent="@android:style/Theme.Dialog"> 
    <!-- other attributes --> 
    <item name="android:backgroundDimEnabled">false</item> 
</style> 
+1

完美的作品:) – 2013-12-01 02:01:24

+0

看起來像這應該是被接受的答案。除了刪除灰色部分之外,接受答案中的xml還會更改屏幕截圖的橙色部分的顏色。 – 2014-02-19 01:41:51

+0

這是正確的答案。 – KingWu 2016-02-24 06:22:50

2

重寫的onCreate和設置樣式應該工作。

@Override 
public void onCreate(Bundle savedInstance){ 
    super.onCreate(savedInstance); 
    setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent); 
} 
1

這些誰使用「onCreateDialog」 AlertDialog建設者,而不是「onCreateView」可以像下面的代碼分配的主題。完整的主題可以從R.style找到。不要忘記,它們中的一些最近支持並且不適用於舊設備手機。

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent); 
     View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null); 
     builder.setView(view); 

     return builder.create(); 
    } 
24

我希望有一個透明背景爲我DialogFragment,並在代碼,這工作得很好:

@Override 
public void onStart() { 
    super.onStart(); 

    Window window = getDialog().getWindow(); 
    window.setBackgroundDrawableResource(android.R.color.transparent); 
} 

當然,你可以指定使用setBackgroundDrawable()setBackgroundDrawableResource()任何顏色或Drawable

這個工程至少在onStart(),但不是在onCreate(),而不一定在onCreateView()it seems

這就是說,在大多數情況下,它可能是清潔要做到這一點in XML, using styles,沿着這些線路:

<style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog"> 
    <item name="android:windowBackground">@android:color/transparent</item> 
</style> 
+0

爲我工作,夥計們是把戲,謝謝你,救了我的時間。 – 2014-11-05 11:24:33

9

爲了得到一個完全透明的對話框中,可以設置在onCreateView以下

  • setBackgroundDrawable到Color.TRANSPARENT
  • setDimAmount爲0

查看代碼示例:

public class TextEditor extends DialogFragment { 

    public TextEditor() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_text_editor, container); 

     // make dialog itself transparent 
     getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

     // remove background dim 
     getDialog().getWindow().setDimAmount(0); 

     //[add more custom code here...] 

     return view; 
    } 
} 
0

Jul的答案對我來說幾乎是好的。但是在使用AlertDialog.Builder時似乎不起作用。

我不得不在這裏設置樣式:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog);