2016-12-10 59 views
-1

我知道這很糟糕。從幾個小時我試圖改變themeAlertDialog但不這樣做。無法更改對話框背景?

父主題:

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="android:background">@color/primary_background</item> 
    <item name="android:colorPrimaryDark">@color/primary_background</item> 
    <item name="android:navigationBarColor">@color/primary_background</item> 
    <item name="android:popupBackground">@color/primary_background</item> 
    <item name="android:alertDialogTheme">@style/myDialog</item> 
</style> 

對話主題:

<style name="myDialog" parent="Theme.AppCompat.Dialog"> 
    <item name="dialogPreferredPadding">@dimen/dialog_padding</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowBackground">@color/white</item> 
</style> 

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog)); 

我無法弄清楚什麼錯誤。我認爲這很容易。

回答

0

而不是做它xml風格,你可以簡單地編程acheive它下面的方式。

創建你在AlertDialog,以顯示你的XML文件。

例如:名稱是abc_dialog.xml

現在做這樣

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.abc_dialog, (ViewGroup) getCurrentFocus()); 

,並設置視圖您AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(dialoglayout); 
builder.show(); 
+0

如果我需要改變背景顏色,文本顏色? –

+0

@AnkurKhandelwal改變背景顏色,因爲我建議你可以很容易地改變你創建'abc_dialog.xml'文件中的背景顏色,正如我在答案中所建議的那樣。 – Ironman

+0

這樣我就必須改變每個對話框的背景顏色。 –

0

I.爲對話框背景聲明自定義可繪製background_dialog.xml。

<?xml version="1.0" encoding="utf-8"?> 
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml --> 
<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:insetLeft="16dp" 
    android:insetTop="16dp" 
    android:insetRight="16dp" 
    android:insetBottom="16dp"> 

    <shape android:shape="rectangle"> 
     <corners android:radius="2dp" /> 
     <solid android:color="@color/indigo" /> 
    </shape> 

</inset> 

二,在styles.xml文件中聲明自定義樣式。

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <!--buttons color--> 
    <item name="colorAccent">@color/pink</item> 
    <!--title and message color--> 
    <item name="android:textColorPrimary">@android:color/white</item> 
    <!--dialog background--> 
    <item name="android:windowBackground">@drawable/background_dialog</item> 
</style> 

III。在AlertDialog.Builder中創建對話框並使用樣式作爲參數。

AlertDialog.Builder builder = 
     new AlertDialog.Builder(this, R.style.MyDialogTheme); 
... 
AlertDialog dialog = builder.create(); 
// display dialog 
dialog.show(); 

結算這document瞭解更多詳情。

OR

您可以設置對話框中的自定義視圖編程像下面的方式。

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus()); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(dialoglayout); 
builder.show(); 

在此之後,你可以得到分量的參考如下

Button btn = (Button) dialoglayout.findViewById(R.id.button_id); 
+0

這隻能改變確定和取消書面的底線。 –

+0

爲什麼主題解決方案無法正常工作? –

+0

嘗試設置'colorBackground'屬性,像這樣'' @ color/white' ' –