Android 无边框对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1883425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Borderless Dialog
提问by kwogger
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.
我使用 AlertDialog.Builder 创建了一个 AlertDialog,但对话框边框在屏幕上占用了太多空间。如何去除边框?我曾尝试使用另一个 Activity 来模拟具有透明背景的对话框,但是该对话框被重复使用,并且每次创建一个新的 Activity 都会引入大量延迟。
The answer from herementions that it can be found in the ApiDemos, but i can't seem to find it.
这里的答案提到它可以在 ApiDemos 中找到,但我似乎找不到它。
回答by kwogger
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.
好吧,我来回答我自己的问题。基本上,不是使用 AlertDialog.Builder,而是使用它的构造函数创建一个常规 Dialog,并使用合适的主题而不是默认的 Dialog 主题。
So your code would look something like this:
所以你的代码看起来像这样:
Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
Hope this helps someone else.
希望这对其他人有帮助。
回答by jokernk
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
themes.xml // 位于项目 /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
color.xml // 也位于那里
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent_color">#00000000</color>
</resource>
回答by jonasb
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:
android.R.style.Theme_Translucent_NoTitleBar
如果您希望对话框全屏显示,请使用作品。另一种方法是创建自己的样式,如下所示:
<style
name="Theme_Dialog_Translucent"
parent="android:Theme.Dialog">
<item
name="android:windowBackground">@null</item>
</style>
回答by Ishanssi
try this :D
试试这个:D
Dialog popUpView= new Dialog(this);
popUpView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
回答by douarbou
if you have 2 border you need to use a ContextThemeWrapper, which it will show only one border as you would like :)
如果您有 2 个边框,则需要使用 ContextThemeWrapper,它将只显示您想要的一个边框:)
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
回答by TomBombadil
I added a transparent pixel to drawable and used the following code :
我向 drawable 添加了一个透明像素并使用了以下代码:
dialog.getWindow().setBackgroundDrawableResource(R.drawable.transpix);
回答by bung_julio
You can ask the builder to enforce inverse background. Worked for me to display a borderless splash screen with a png source.
您可以要求构建器强制执行反向背景。为我工作以显示带有 png 源的无边框启动画面。
回答by greenspand
In your resources file create a xml file named for e.g. null_image.xml, with the following content:
在您的资源文件中创建一个以 null_image.xml 命名的 xml 文件,内容如下:
<?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:
在您的 java 代码中,获取对话框窗口并将 xml 文件设置为可绘制资源,如下所示:取决于您的上下文:
Dialog dialog = new Dialog(getContext());
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(R.drawable.null_image);
That's it, enjoy.
就这样,享受。