Android: how to create a transparent dialog-themed activity

前端 未结 6 1273
清歌不尽
清歌不尽 2020-12-03 05:04

My original goal here was a modal dialog, but you know, Android didn\'t support this type of dialog. Alternatively, building a dialog-themed activity would possibly work.

相关标签:
6条回答
  • 2020-12-03 05:50

    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.

    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    
    0 讨论(0)
  • 2020-12-03 05:54

    The easiest way that I have found is to set the activity's theme in the AndroidManifest to android:theme="@android:style/Theme.Holo.Dialog" then in the activity's onCreate method call getWindow().setBackgroundDrawable(new ColorDrawable(0));

    0 讨论(0)
  • 2020-12-03 05:55

    Just a quick update on @user824330 answer. Since you have the AlertDialog.Builder class, why not using it properly? 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();
    }
    

    }

    0 讨论(0)
  • 2020-12-03 05:59

    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:

    floating calculator

    0 讨论(0)
  • 2020-12-03 06:00

    You might be better off building a DialogFragment and customize the dialog with setcontentview.

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

    0 讨论(0)
  • 2020-12-03 06:04
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    

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

    0 讨论(0)
提交回复
热议问题