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" 
相关问题