2013-07-15 73 views
2

我曾經爲將活動的背景轉爲透明而苦惱。我會正確設置佈局背景爲#00000000,我會在onCreate方法中正確設置getWindow().setBackgroundDrawable(new ColorDrawable(0))。但是,通過這兩個變化,我總是會得到一個灰黑色的容器來保存我的佈局。但之後我發現我需要編輯清單中的活動標記以添加android:theme="@android:style/Theme.Holo.Dialog"。瞧!就這些。給AlertDialog自定義或透明背景

但現在我需要將AlertDialog的背景變爲透明。這裏有很多建議,我嘗試了很多。下面提供了我的最新配置。但我總是會遇到同樣的問題,我曾經有過這樣的活動:容納我的佈局的灰黑色容器。所以現在,我的問題是:我如何在清單文件中爲我的自定義對話框添加android:theme="@android:style/Theme.Holo.Dialog"

當前代碼:

public void showMyDialog() { 
    ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomDialog); 
    AlertDialog.Builder builder = new AlertDialog.Builder(ctw); 
    LayoutInflater inflater = (LayoutInflater) ctw.getSystemService(LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.dialog_layout, 
    (ViewGroup) findViewById(R.id.pr_root)); 
    builder.setView(view); 
    builder.show(); 
} 

風格:

<style name="CustomDialog" parent="android:Theme.Holo.Dialog"> 
        <item name="android:windowIsTranslucent">true</item> 
        <item name="android:windowBackground">@android:color/transparent</item> 
    </style> 

<!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
        <item name="android:alertDialogStyle">@style/CustomDialog</item> 
        <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    </style> 

當然dialog_layout的是一個典型的佈局.xml文件。

回答

2

使用對話框而不是AlertDialog.Builder,因此使用setContentView而不是setView。

+0

所以這種作品。它拿出了灰黑色的背景。但是有一個問題。現在我的對話框頂部有這個藍色/青色條。我如何刪除該欄? –

+0

使用'builder.getWindow()。requestFeature(Window.FEATURE_NO_TITLE);' –

+0

不幸的是,對話框不能很好地與Contextual Action Bar配合使用,並且會錯位。我花了一段時間才發現Dialog是罪魁禍首。 – Saket