Android无边框对话框

时间:2009-12-10 19:37:50

标签: android dialog

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

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

8 个答案:

答案 0 :(得分:63)

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

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

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

希望这有助于其他人。

答案 1 :(得分:33)

这是我的解决方案,以获得仅显示您的内容的对话框。

    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 //位于project / 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>

答案 2 :(得分:24)

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

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

答案 3 :(得分:7)

试试这个:D

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

答案 4 :(得分:2)

我添加了一个透明像素来绘制并使用以下代码:

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

答案 5 :(得分:2)

如果您有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);

答案 6 :(得分:1)

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

答案 7 :(得分:0)

在您的资源文件中创建一个名为eg的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);

那就是它,享受。