Android Borderless Dialog

2019-01-10 15:06发布

I have created an AlertDialog using AlertDialog.Builder, but the Dialog border takes up too much space on the screen. How do I remove the border? I have tried using another Activity to emulate the dialog with a transparent background, but the dialog is used repeatedly, and creating a new Activity every time introduces a significant amount of lag.

The answer from here mentions that it can be found in the ApiDemos, but i can't seem to find it.

8条回答
仙女界的扛把子
2楼-- · 2019-01-10 15:21

Using android.R.style.Theme_Translucent_NoTitleBar works if you want the dialog to be full screen. An alternative is to create your own style, like so:

<style
    name="Theme_Dialog_Translucent"
    parent="android:Theme.Dialog">
    <item
        name="android:windowBackground">@null</item>
</style>
查看更多
狗以群分
3楼-- · 2019-01-10 15:23

I added a transparent pixel to drawable and used the following code :

dialog.getWindow().setBackgroundDrawableResource(R.drawable.transpix);
查看更多
唯我独甜
4楼-- · 2019-01-10 15:25

In your resources file create a xml file named for e.g. null_image.xml, with the following content:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#0000" />

<size
    android:height="1dp"
    android:width="1dp" />

 </shape>

In your java code, fetch the dialog window and set the xml file as the drawable resource, like this: Depending on your context:

Dialog dialog = new Dialog(getContext());
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(R.drawable.null_image);

That's it, enjoy.

查看更多
仙女界的扛把子
5楼-- · 2019-01-10 15:35

Alright, I'll answer my own question. Basically, instead of using AlertDialog.Builder, create a regular Dialog using it's constructor, and use a suitable theme instead of the default Dialog theme.

So your code would look something like this:

Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);

Hope this helps someone else.

查看更多
Ridiculous、
6楼-- · 2019-01-10 15:36

You can ask the builder to enforce inverse background. Worked for me to display a borderless splash screen with a png source.

查看更多
倾城 Initia
7楼-- · 2019-01-10 15:39

Here is my solution, to get a dialog that shows only your content.

    Dialog dialog = new Dialog(this,R.style.ThemeDialogCustom);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //you can move the dialog, so that is not centered
    // dialog.getWindow().getAttributes().y = 50; //50 should be based on density

    dialog.setContentView(yourLinearLayout);
    dialog.setCancelable(true);
    //dialog.setOnCancelListener(cancelListener);
    dialog.show();

themes.xml // located in project /res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="ThemeDialogCustom">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@color/transparent_color</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
   </style>
</resources>

colors.xml // also located there

<?xml version="1.0" encoding="utf-8"?>
<resources>
       <color name="transparent_color">#00000000</color>
</resource>
查看更多
登录 后发表回答