2015-04-07 303 views
0

我想使用自定義對話框如何設置AlertDialog背景顏色

我試圖設置主題AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.appContext,r.style.my.Theme);,但它不是改變背景來改變我的AlertDialog背景顏色

與出顏色。

+0

檢查是否添加一個可繪製作爲背景的作品。 builder.getWindow()。setBackgroundDrawableResource(R.drawable.menubackground); –

回答

0

下創建RES /值新的Android XML資源文件/

你要創建一個新的風格,從默認alertdialog主題繼承:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog"> 
    <item name="android:bottomBright">@color/white</item> 
    <item name="android:bottomDark">@color/white</item> 
    <item name="android:bottomMedium">@color/white</item> 
    <item name="android:centerBright">@color/white</item> 
    <item name="android:centerDark">@color/white</item> 
    <item name="android:centerMedium">@color/white</item> 
    <item name="android:fullBright">@color/orange</item> 
    <item name="android:fullDark">@color/orange</item> 
    <item name="android:topBright">@color/blue</item> 
    <item name="android:topDark">@color/blue</item> 
</style> 

您可以指定顏色或可以爲AlertDialog的每個部分繪製。 AlertDialog將通過組合3個drawable/colors(頂部,中間,底部)或單個drawable/color(full)來構建它的顯示。

在自己覆蓋的主題安卓:alertDialogStyle風格(你可以在同一個XML文件中做到這一點):

<style name="MyTheme"> 
    <item name="android:alertDialogStyle">@style/CustomDialogTheme</item> 
</style> 

應用標籤內重寫應用程序的主題在AndroidManifest

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/MyTheme"> 

既然已經定義了應用程序的主題,那麼主題中覆蓋的任何屬性都會在整個應用程序中產生共鳴。 例如,如果你也想改變你的應用程序的標題欄的顏色: 你要首先定義一個新的風格,從默認的屬性繼承

<style name="MyBackground" parent="android:WindowTitleBackground">  
    <item name="android:background">@color/blue</item> 
</style> 

,只是添加新的重寫項目的主題:

<style name="MyTheme"> 
    <item name="android:windowTitleBackgroundStyle">@style/MyBackground</item> 
    <item name="android:alertDialogStyle">@style/CustomDialogTheme</item> 
</style> 
0

您是否嘗試更改孔對話窗口的背景顏色?

未經測試,只查找API參考文獻。

AlertDialog正在擴展Dialog。

在對話框是方法:

getWindow() 

的窗口http://developer.android.com/reference/android/view/Window.html參考示出了方法:

setBackgroundDrawable (Drawable drawable) 
and 
setBackgroundDrawableResource (int resId) 

可以工作。

+0

這是改變整個窗口的顏色,我只想改變提醒對話部分不是整個屏幕。 –

+0

好的;)是一個嘗試 –

0
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Title") 
     .setCancelable(false) 
     .setPositiveButton("Go", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 
      EditText textBox = (EditText) findViewById(R.id.textbox); 
      doStuff(); 
     } 
    }); 

    FrameLayout f1 = (FrameLayout)findViewById(R.id.body /*CURRENTLY an ERROR*/); 
    f1.addView(findViewById(R.layout.dialog_view)); 


    LayoutInflater inflater = getLayoutInflater(); 
    FrameLayout f1 = (FrameLayout)alert.findViewById(android.R.id.body); 
    f1.addView(inflater.inflate(R.layout.dialog_view, f1, false)); 

AlertDialog alert = builder.create(); 
    alert.show(); 

此代碼將您的對話框中設置到名爲dialog_view從那裏你可以做你想做的是自定義佈局什麼都自定義佈局文件。

你要設置的背景在自定義佈局,以便在佈局設置背景根佈局對象

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/dialog_layout_root" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="10dp" 
       android:background="#000000" 
       />