安卓:无阴影的对话框

15
我该如何去除对话框周围的"shadowBox"效果,使背景不再变淡?
我知道我可以创建自定义视图,但假设我想保持简单。我有一个仅包含TextView和2个按钮的对话框xml布局,并且我只想停止这一个对话框的淡化效果。
5个回答

45

尝试:

dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);

11
谢谢。它有效。LayoutParams必须来自android.view.WindowManager。 - Andrei Aulaska
1
此常量已被弃用,目标SDK版本为22。 - goRGon

9
values文件夹中创建style.xml,并添加以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="progress_dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">#00ffffff</item> 
</style>

最后,将这个样式应用到你的对话框中。

嗯,我将其应用为style="@style/styled_dialog"到我的dialog.xml中的linearLayout上,但什么也没有发生。 - yosh
你使用的是哪种对话框?进度条对话框还是警告对话框?我在使用ProgressDialog时指定了样式并进行了实例化。 - Aman Alam
1
@Sheikh-Aman 我正在使用 Alert 对话框,但我找不到如何为其添加样式的方法。 - yosh

6

只需简单地使用:

 dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

这也会使对话框的背景透明。我认为那不是想要的。 - Don Hatch

2

这里提供一个样例类:

public void showDialog () {

    Dialog dialog = new Dialog(this);
    Window window = dialog.getWindow();
    window.setBackgroundDrawableResource(android.R.color.transparent);
    window.requestFeature(window.FEATURE_NO_TITLE);

    dialog.setContentView(R.layout.sample);
    ImageView imggameover, imgplayagain;
    imggameover = (ImageView) dialog.findViewById(R.id.gameover);
    imgplayagain = (ImageView) dialog.findViewById(R.id.playagain);
    imgplayagain.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(getApplicationContext(), random.class));
            onStop();
            finish();
        }
    });
    dialog.show();
}

对话框的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gameoverlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent"
    >
    <ImageView
        android:id="@+id/gameover"
        android:layout_width="225dip"
        android:layout_height="160dip"
        android:background="@drawable/gameover"
        ></ImageView>
    <ImageView
        android:id="@+id/playagain"
        android:layout_width="160dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dip"
        android:layout_marginTop="100dip"
        android:background="@drawable/playagain"
        ></ImageView>
</RelativeLayout>

有用的东西!但这并不能解决背景逐渐消失的问题。它只是移除了对话框窗口 :) - yosh

0

只是让你知道,通过在主题中编写以下内容,也可以更改暗淡程度:

<item name="android:backgroundDimAmount">0.5</item>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接