简体   繁体   中英

Android Dialog fragment eliminate black borders (top & bottom)

Need to eliminate top and bottom borders from a dialog fragment. How do you do it?

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(true);

    LayoutInflater inflater = LayoutInflater.from(activity);
    View view = inflater.inflate(R.layout.variants_dialog, null);

    // setup views
    setupListView(view);
    ...

    builder.setView(view);
    return builder.create(); // HERE I HAVE TOP & BOTTOM BLACK BORDERS

This doesnt do nothing:

        builder.setView(view);
        AlertDialog result = builder.create();
        result.getWindow().setBackgroundDrawable(new ColorDrawable());
        return result;

There is no such method:

         dialog.setView(layout, 0, 0, 0, 0);

I am not that familiar with DialogFragments, however, with regular dialogs to do this you would usually R.value.styles and add:

 <style name="myDialog" parent="@android:style/Theme.Dialog">
    <item name="android:buttonStyle">@style/Button</item>
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:textColor">#FFFFFFFF</item>
    <item name="android:shadowColor">#cdc9c9</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">-1</item>
    <item name="android:shadowRadius">0.5</item>
</style>

And set your dialog to have the stile myDialog . However, with dialogFragments , it seems you need to define the style when you initialise it, DialogFragment.STYLE_NO_FRAME; , Hope that helps somewhat, although I know it wasn't a complete answer.

FOUND SOLUTION!!

There is no

setView(layout, 0, 0, 0, 0);

on builder from dialogFragment, but AlertDialog has this method.. so instead of returning

builder.create();

do this

//dont set view for builder!
AlertDialog result = builder.create();
result.setView(view, 0, 0, 0, 0);
return result;

这对我有用:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

I had a similar problem not too long ago. The problem is that the the Builder doesn't let you play with the padding of the dialog at all. My work around was to create a subclass of the AlertDialog:

final class PaddinglessDialog extends AlertDialog {
    public PaddinglessDialog(Context context, int theme) {
        super(context, theme);
    }
}

Then i went on to use it like this:

PaddinglessDialog alertDialog = new PaddinglessDialog(this, android.R.style.Theme_Holo_Light_Panel);
View layout = LayoutInflater.from(this).inflate(R.layout.my_dialog_layout, ...);
alertDialog.setView(layout, 0, 0, 0, 0);
alertDialog.setCanceledOnTouchOutside(false);
...
dialog = alertDialog;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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