【问题标题】:Android Borderless DialogAndroid 无边框对话框
【发布时间】:2010-12-25 09:37:55
【问题描述】:

我使用 AlertDialog.Builder 创建了一个 AlertDialog,但对话框边框在屏幕上占用了太多空间。如何去除边框?我曾尝试使用另一个 Activity 来模拟具有透明背景的对话框,但该对话框被重复使用,并且每次创建一个新 Activity 都会引入大量延迟。

here 的回答提到可以在 ApiDemos 中找到它,但我似乎找不到它。

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    好的,我会回答我自己的问题。基本上,不是使用 AlertDialog.Builder,而是使用它的构造函数创建一个常规 Dialog,并使用合适的主题而不是默认的 Dialog 主题。

    所以你的代码看起来像这样:

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

    希望这对其他人有所帮助。

    【讨论】:

    • 如果我们这样做,那么 dialog.setCanceledOnTouchOutside(true);不会有效果...那怎么处理呢?
    • 这正是我想要的。几个小时以来,我一直在与对话框创建系统和几十个写得很糟糕的教程搏斗,而你的一行代码就是我所需要的。谢谢!
    • 但它使对话框全屏..当我编写上述代码时如何防止它不全屏
    • gist 显示没有标题或边框的自定义浮动(弹出)对话框:gist.github.com/2643546
    【解决方案2】:

    这是我的解决方案,得到一个只显示您的内容的对话框。

        Dialog dialog = new Dialog(this,R.style.ThemeDialogCustom);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    
        //you can move the dialog, so that is not centered
        // dialog.getWindow().getAttributes().y = 50; //50 should be based on density
    
        dialog.setContentView(yourLinearLayout);
        dialog.setCancelable(true);
        //dialog.setOnCancelListener(cancelListener);
        dialog.show();
    

    themes.xml // 位于项目 /res/values

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <style name="ThemeDialogCustom">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowBackground">@color/transparent_color</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
       </style>
    </resources>
    

    colors.xml // 也在那里

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
           <color name="transparent_color">#00000000</color>
    </resource>
    

    【讨论】:

    • 这是一个不错的解决方案。它通过简单地为您提供相同的对话框完美地解决了问题,但没有边框!!!这是一个比公认的更合适的解决方案。
    • 感谢您提供正确的解决方案。您也可以添加 dialog.setCanceledOnTouchOutside(true);关闭外部对话框触摸上的对话框。
    【解决方案3】:

    如果您希望对话框全屏显示,则使用android.R.style.Theme_Translucent_NoTitleBar 有效。另一种方法是创建自己的样式,如下所示:

    <style
        name="Theme_Dialog_Translucent"
        parent="android:Theme.Dialog">
        <item
            name="android:windowBackground">@null</item>
    </style>
    

    【讨论】:

      【解决方案4】:

      试试这个:D

       Dialog popUpView= new Dialog(this);
       popUpView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
      

      【讨论】:

        【解决方案5】:

        我为drawable添加了一个透明像素并使用了以下代码:

        dialog.getWindow().setBackgroundDrawableResource(R.drawable.transpix);
        

        【讨论】:

          【解决方案6】:

          如果你有 2 个边框,你需要使用 ContextThemeWrapper,它只会显示你想要的一个边框:)

          ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
          final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
          AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
          

          【讨论】:

            【解决方案7】:

            您可以要求构建器强制执行反向背景。为我工作以显示带有 png 源的无边框初始屏幕。

            【讨论】:

              【解决方案8】:

              在您的资源文件中创建一个名为例如的 xml 文件。 null_image.xml,内容如下:

              <?xml version="1.0" encoding="utf-8"?>
              <shape xmlns:android="http://schemas.android.com/apk/res/android" >
              
              <solid android:color="#0000" />
              
              <size
                  android:height="1dp"
                  android:width="1dp" />
              
               </shape>
              

              在您的 java 代码中,获取对话框窗口并将 xml 文件设置为可绘制资源,如下所示: 根据您的上下文:

              Dialog dialog = new Dialog(getContext());
              Window window = dialog.getWindow();
              window.setBackgroundDrawableResource(R.drawable.null_image);
              

              就是这样,享受吧。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-03-26
                • 1970-01-01
                • 2022-10-18
                • 1970-01-01
                • 2018-04-24
                • 1970-01-01
                相关资源
                最近更新 更多