2

我有一個使用TabLayout和Fragments的應用程序,但是我的初始登錄屏幕是標準的Activity。當我從登錄屏幕顯示警告對話框時,對話框的外觀與從片段內部顯示一個完全不同。Android:AlertDialog在片段中看起來不同

從登錄屏幕

enter image description here

從內片段的

enter image description here

,我用它來顯示alertDialog的代碼下面的類

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.graphics.Typeface; 
import android.widget.Button; 
import android.widget.TextView; 

class AlertDialogManager { 

    private AlertDialog alertDialog; 
    private Context mContext; 

    public void showAlertDialog(final Activity activity, String title, String message, Boolean status, final Boolean finishOnClose) { 
     // Set our context correctly based on what was passed in activity 
     mContext = (activity.getParent()!=null) ? mContext = activity.getParent() : activity; 

     // Create our alertDialog Builder 
     alertDialog = new AlertDialog.Builder(mContext).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle(title);   

     // Setting Dialog Message 
     alertDialog.setMessage(message); 

     // Setting alert dialog icon 
     if(status != null) alertDialog.setIcon((status) ? R.drawable.icon_check : R.drawable.icon_alert); 

     // Setting OK Button 
     alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // if the user passed in TRUE for the finishOnClose param - we call try onBackPressed first and if that fails, call finish() 
       if (finishOnClose) { 
        try { 
         activity.onBackPressed(); 
        } catch (Exception e) { 
         activity.finish(); 
        }    
       } 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 
} 

要我用下面的活動表明一個:

// At the top of my activity I declare 
private final AlertDialogManager alertDialog = new AlertDialogManager(); 

// Then where I want to show one I use this 
alertDialog.showAlertDialog(MyActivity.this, "Title", "Message", false, false); 

要顯示一個在一個片段我使用以下命令:

// At the top of my fragment I declare 
private final AlertDialogManager alertDialog = new AlertDialogManager(); 

alertDialog.showAlertDialog(getActivity(), "Title", "Message", false, false); 

任何人都可以解釋爲什麼我會完全得到2從Activity和Fragment進行調用時,我的對話框中有不同的「主題」?我很難過。

謝謝!!!

+0

兩個不同的Android版本? – Blackbelt

+0

@Blackbelt - 我該如何檢查?我有compileSdkVersion 23,minSdkVersion 19和targetSdkVersion 23 - 我的build.gradle – Phil

+0

我的意思是在你運行應用程序的設備上。您使用的是哪個版本的AlertDialog? 'v7.app.AlertDialog'?檢查您的導入 – Blackbelt

回答

2

您支持的舊API版本是什麼?因爲您可以使用自API 11以來的AlertDialog builde。如果您正在支持舊版本,則必須設置主題。

例子:

ContextThemeWrapper theme; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     theme = new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar); 
    } 
    else { 
     theme= new ContextThemeWrapper(context, android.R.style.Theme_Light_NoTitleBar); 
    } 
    AlertDialog.Builder builder = new AlertDialog.Builder(theme); 

希望這有助於。

+0

我minSdkVersion是19,我的targetSdkVersion是23,我的compileSdkVersion是23 – Phil

+0

也許有調用不同風格的問題,我現在無法想象另一個解決方案,對不起。 –

0

原來我需要添加下面的聲明中我表現爲我登錄活動

android:theme="@style/Theme.AppCompat.NoActionBar"