【问题标题】:How to make an Alert dialog in full screen in Android?如何在Android中全屏制作警报对话框?
【发布时间】:2016-11-27 10:03:51
【问题描述】:

如何在 Android 中全屏制作警报对话框?

【问题讨论】:

  • 这不是在 StackOverflow 上提问的方式。提供您所做的工作,任何教程或您的代码 sn-p。

标签: android fullscreen android-alertdialog


【解决方案1】:

您必须为您的对话框创建自定义样式

在你的 style.xml 中

 <style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>   
    <!-- No backgrounds, titles or window float -->
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

在为您的对话框充气时设置此主题

dialog = new Dialog(this, R.style.DialogTheme);

【讨论】:

  • 这也改变了对话框的风格。
【解决方案2】:

试试下面的代码

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

【讨论】:

  • 它有效,但这也改变了主题。
【解决方案3】:

如果您使用 DialogFragment 构建 AlertDialog,您可以在 onResume() 中的 LayoutParams 上设置 MATCH_PARENT:

@Override
public void onResume() {
    super.onResume();
    ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.MATCH_PARENT;
    getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}

【讨论】:

  • 虽然这确实有效,但它并不适合 MATCH_PARENT
【解决方案4】:

【方法一 |使用自定义样式】

在样式文件中:

<style name="myFullscreenAlertDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">      //no actionbar, but status bar exists
    <item name="android:windowFullscreen">true</item>                       //remove status bar
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>        //smooth animation
    <item name="colorAccent">@color/colorAccent</item>                      //change button text color
</style>

在java文件中:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.myFullscreenAlertDialogStyle);   //second argument

【方法二 |使用内置样式】

在java文件中:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
  • 此方法会使按钮文字变为绿色,您可以使用dialog.getButton().setTextColor()进行更改。

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2019-01-25
    相关资源
    最近更新 更多