简体   繁体   English

Android:如何创建透明的对话框主题活动

[英]Android: how to create a transparent dialog-themed activity

My original goal here was a modal dialog, but you know, Android didn't support this type of dialog.我最初的目标是一个模态对话框,但你知道,Android 不支持这种类型的对话框。 Alternatively, building a dialog-themed activity would possibly work.或者,构建一个以对话为主题的活动可能会奏效。
Here's code,这是代码,

public class DialogActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    setTheme(android.R.style.Theme_Dialog);

    super.onCreate(savedInstanceState);

    requestWindowFeature (Window.FEATURE_NO_TITLE);

    Button yesBtn = new Button(this);
    yesBtn.setText("Yes");
    yesBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DialogActivity.this.finish();

        }

    });

    this.setContentView(yesBtn);
}

However, the background was always black, that really confusing.然而,背景总是黑色的,这真的很混乱。 People got same problem,人们遇到了同样的问题,
http://code.google.com/p/android/issues/detail?id=4394 http://code.google.com/p/android/issues/detail?id=4394
I just wanna make it look more dialog-like for user.我只是想让它看起来更像用户对话。 So, how to get this DialogActivity transparent and never cover underlying screen?那么,如何让这个 DialogActivity 透明并且从不覆盖底层屏幕?
Thanks.谢谢。

This was how I created the style这就是我创建样式的方式

<style name="CustomDialogTheme" parent="android:Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
</style>

You can create a tranparent dialog by this.你可以通过这个创建一个透明的对话框。

public class DialogActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(" ");
        alertDialog.setMessage("");
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setButton("Accept", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {                
            }
        });
        alertDialog.setButton2("Deny", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertDialog.show();
    }
}

After this just put a line in AndroidManifest.xml, where you define your activity in manifest.之后只需在 AndroidManifest.xml 中添加一行,在清单中定义您的活动。

android:theme="@android:style/Theme.Translucent.NoTitleBar"

我发现的最简单的方法是将 AndroidManifest 中的活动主题设置为android:theme="@android:style/Theme.Holo.Dialog"然后在活动的 onCreate 方法中调用getWindow().setBackgroundDrawable(new ColorDrawable(0));

Just one thing to add to all the answers here.只需要将一件事添加到此处的所有答案中。 To achieve a full dialog like behaviour, you can reposition the dialog by changing the window layout params:要实现类似行为的完整对话框,您可以通过更改窗口布局参数来重新定位对话框:

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;

this.getWindow().setAttributes(params);

We used a similar technique to achieve a floating activity effect in the Tooleap SDK:我们在Tooleap SDK中使用了类似的技术来实现浮动活动效果:

浮动计算器

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

and import android.graphics.drawable.ColorDrawable; on top :)import android.graphics.drawable.ColorDrawable; on top :) import android.graphics.drawable.ColorDrawable; on top :)

Just a quick update on @user824330 answer.只是对@ user824330 答案的快速更新。 Since you have the AlertDialog.Builder class, why not using it properly?既然你有 AlertDialog.Builder 类,为什么不正确使用它呢? Many methods are now deprecated.许多方法现在已被弃用。 The up-to-date code would be similar to this:最新的代码类似于:

public class DialogActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("title")
       .setMessage("message")
       .setIcon(R.drawable.ic_launcher)
       .setPositiveButton("Yes", new OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {}
       })
       .setNegativeButton("No", new OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {}
       });

    final AlertDialog dialog = builder.create();
    dialog.show();
}

} }

You might be better off building a DialogFragment and customize the dialog with setcontentview.您最好构建一个 DialogFragment 并使用 setcontentview 自定义对话框。

http://developer.android.com/reference/android/app/DialogFragment.html http://developer.android.com/reference/android/app/DialogFragment.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM