AlertDialog: How To Remove Black Borders Above and Below View

后端 未结 5 1711
难免孤独
难免孤独 2020-12-05 06:47

This question has been asked before: AlertDialog custom title has black border

But was not answered satisfactorily - and is missing some information.


I

相关标签:
5条回答
  • 2020-12-05 07:22

    Just to make Steve's answer more clear, this can be done easily. For example in my case the view I was setting in the dialog was a WebView.

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        WebView webView = new WebView(getActivity());
        webView.loadUrl(" url to show ");
    
    
        OnClickListener clickListenerOk = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ...
            }
        };
    
        OnClickListener clickListenerCancel = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ...
            }
        };
    
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
    
                .setPositiveButton("OK", clickListenerOk)
    
                .setNegativeButton("Cancel",clickListenerCancel)
    
                .create();
    
        dialog.setView(webView, 0, 0, 0, 0);
    
        return dialog;
    }
    
    0 讨论(0)
  • 2020-12-05 07:25
    dialog.setInverseBackgroundForced(true);
    

    use the above in your code to remove the border of the alert dialog.

    Refer this LINK for InverseBackgroundForced.

    UPDATED Try this code::::

    public class Welcome  extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.welcome);
    
            AlertDialog.Builder builder = new AlertDialog.Builder(Welcome.this);
            LayoutInflater _inflater = LayoutInflater.from(Welcome.this);
            View view = _inflater.inflate(R.layout.welcomedialog,null);
            builder.setView(view);
    
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
    

    Note:: Also try by removing android:padding="40px" from welcomedialog.xml.

    0 讨论(0)
  • 2020-12-05 07:25

    In my case, that border was caused by the theme of the parent Activity for the AlertDialog. To get rid of the border completely, give it a different theme (in this case, Holo):

    AlertDialog.Builder builder = new AlertDialog.Builder(
                                    new ContextThemeWrapper(this, android.R.style.Theme_Holo)
                                  );
    

    This fixed it for me. Hope this helps!

    0 讨论(0)
  • 2020-12-05 07:35
    setBackgroundDrawable(new ColorDrawable(0));
    

    Call this in your dialog.

    0 讨论(0)
  • 2020-12-05 07:44

    If you look at the AlertDialog class source you'll see most of the methods are simply proxy methods (facade) around private AlertController mAlert.

    Looking at the AlertController class source you'll see 4 interesting member variables:

    private int mViewSpacingLeft;
    private int mViewSpacingTop;
    private int mViewSpacingRight;
    private int mViewSpacingBottom;
    private boolean mViewSpacingSpecified = false;
    

    Setting mViewSpacingSpecified to true will remove the borders on the top and bottom of the dialog.

    This is done properly by changing this line:

    dialog.setView(layout);
    

    to:

    dialog.setView(layout, 0, 0, 0, 0);
    
    0 讨论(0)
提交回复
热议问题