2011-09-30 80 views
15

我正在開發一個簡單的演示。在這個演示中,我只是創建一個簡單的自定義警報對話框。它工作正常。更改構建版本後,Android自定義警報對話框顯示錯誤

它顯示了當我在1.6中生成應用程序時的完美結果,但是當我將Android版本從1.6更改爲2.2時,它顯示了意外的結果。它不顯示我顯示自定義警報對話框的背景屏幕。

這是我的xml文件。自定義對話框主題文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CustomDialogTheme" parent="@android:style/AlertDialog"> 
     <item name="android:windowFrame">@null</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="android:backgroundDimEnabled">true</item> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:windowNoTitle">true</item> 
     <item name="android:windowAnimationStyle">@android:style/Theme.Dialog</item> 
    </style> 
</resources> 

這裏是我的CustomConfirmOkDialog類

package com.utility.org; 

import android.app.Activity; 
import android.app.Dialog; 
import android.view.View; 
import android.view.Window; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

    public class CustomConfirmOkDialog extends Dialog implements OnClickListener 
    { 
     private Button okButton = null; 
     private TextView infoText=null,confirmBody=null; 
     private int errorMessage=0; 
     @SuppressWarnings("unused") 
     private Activity activity; 

     public CustomConfirmOkDialog(Activity context,int customdialogtheme,int errorMessage) 
     { 
      super(context,customdialogtheme); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.confirm_ok); 
      this.errorMessage = errorMessage; 
      this.activity = context; 
      initControls(); 
     } 

     private void initControls() 
     { 
      okButton = (Button) findViewById(R.id.ok_button); 
      okButton.setOnClickListener(this); 

      infoText = (TextView)findViewById(R.id.infoText); 
      confirmBody = (TextView)findViewById(R.id.confirmBody); 

      switch (this.errorMessage) 
      { 

       case Utility.INVALID_USERNAME_PASSWORD: 
        try 
        { 
         infoText.setText(R.string.signIn); 
         confirmBody.setText(R.string.invalidUsernameAndPassword); 
        } 
        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
        break; 


       default: 
        break; 
      } 
     } 
     public void onClick(View v) 
     { 
      dismiss(); 
     } 
    } 

使用下面的代碼從我的主要活動中調用這個類。

CustomConfirmOkDialog dialog = new CustomConfirmOkDialog(MainActivity.this, R.style.CustomDialogTheme, Utility.INVALID_USERNAME_PASSWORD); 
dialog.show(); 

enter image description here enter image description here

在這裏,你可以清楚地發現,1st image節目的背景。它在android 1.6版本中構建,而2nd image不顯示背景。它顯示整個黑屏。它的內置版本爲android 2.2。如果有人能解決這個問題,我非常感謝。

任何人都可以幫助我解決這個簡單而愚蠢的問題嗎?

在此先感謝。

+1

當你說改變版本,你的意思是目標版本,最低版本還是最高版本?或者你是否只是指具有更高版本的不同設備? – Gallal

+0

我正在談論項目構建目標版本。 –

+0

愚蠢的評論,但我已經面臨同樣的問題,所以......你是否在使用sdk 2.2模擬器測試演示導致設備或模擬器導致有時視圖不能完美顯示時運行較高版本的應用程序來降低版本模擬器或設備。 – MKJParekh

回答

2

它通過更改Custom Dialog Theme xml文件中的以下代碼來解決我的問題。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CustomDialogTheme" parent="@android:style/Theme.Translucent.NoTitleBar"> 
     <item name="android:windowFrame">@null</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="android:backgroundDimEnabled">true</item> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:windowNoTitle">true</item> 
    </style> 
</resources> 
0

顯然,這是一個known issue

只有當您嘗試從框架主題繼承時纔會發生這種情況。 直接使用@android:style仍然會將它們視爲非全屏的 ,這會按預期穿透黑色背景。

一種解決方法是以接近空白的主題(如Panel或 半透明)開始,然後在自己的佈局中呈現所需內容(例如 對話邊等)。

想了想,我自己還沒有完全理解這個解決方案。

實際上,我不確定他們是否在談論你看到的完全相同的錯誤,因爲他們談論的是它不適用於舊版本的sdk(不是像你的那樣的新版本)。請參閱bug report

1

我也遇到了同樣的問題。問題是,當我叫對話框類

對話框(上下文的背景下,INT的ThemeID)

它會隱藏背景動作的構造。我發現的唯一的解決方案是不調用此構造函數,而不是隻調用

對話框(上下文的背景下)

,並設置你的風格佈局文件。

因此,在你的代碼,只寫

超(上下文)

,而不是

超(上下文的ThemeID);

+1

它沒有給出我想要的正確結果。 –