2014-10-08 109 views
0

我已經搜索了這個問題的答案,但沒有答案幫助我。用透明背景創建一個DialogFragment

問題是我有一個DialogFragment,當用戶添加一個小部件(作爲WidgetConfig的一部分)時顯示。它看起來像這樣:

​​

該對話框像這樣創建的;

調用活動:

public class AppWidgetConfigure extends Activity { 

    private void setUp(){ 

     //Config widget code removed 

     DialogFragment dialog = new ChooserDialog(); 
     dialog.show(getFragmentManager(), "dialog"); 
    } 
} 

的DialogFragment:

public class ChooserDialog extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     choices = getResources().getStringArray(R.array.choices); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
     builder.setTitle(getResources().getString(R.string.widget_dialog_chooser_title)); 
     builder.setPositiveButton(getResources().getString(R.string.widget_dialog_chooser_posBtn), this); 
     builder.setNegativeButton(getResources().getString(R.string.widget_dialog_chooser_negBtn), this); 
     builder.setSingleChoiceItems(choices, -1, this); 
     return builder.create(); 

    } 
} 

我想對話框有一個透明背景。目前,如圖所示,有WidgetConfigure活動作爲背景。

感謝您的幫助。

馬庫斯

+0

Sanghyun卞回答了這個問題非常充分這裏:http://stackoverflow.com/questions/8045556/cant-make-the-custom-dialogfragment-transparent-over-the-片段 雖然最好把代碼放在onCreateView – user2288580 2015-07-29 08:41:55

回答

0

使用下面的代碼,我們可以得到透明AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Translucent);

但在你的情況,你有警告對話框將顯示一些選擇。在這種情況下,您需要使用選擇列表創建具有透明背景的佈局,並將該佈局設置爲對話框。

像這樣

View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null); 
builder.setView(view); 
+0

謝謝你的努力,明天我會試試這個 – Marcus 2014-10-08 18:23:35