如何在 Android 中创建完全自定义的 Dialogue/Popup(更改叠加颜色和对话窗口布局)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3728990/
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
How to create a completely custom Dialogue/Popup in Android (change overlay colour and dialogue window layout)
提问by justinl
I would like to completely re-skin the default dialogue component in Android. Specifically I would like to do this:
我想完全重新设计 Android 中的默认对话组件。具体来说,我想这样做:
Change the semi-transparent overlay background from the default black to a semi-transparent white.
Change the Dialogue window by removing the default windowed frame border, and replacing it with a layout defined in XML (it's just going to be a borderless graphic with floating buttons. no actual frame.)
将半透明叠加背景从默认黑色更改为半透明白色。
通过删除默认的窗口框架边框来更改对话窗口,并将其替换为 XML 中定义的布局(它只是一个带有浮动按钮的无边框图形。没有实际的框架。)
I have seen tutorials about creating a custom layout for within the dialogue box (e.g. http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application), but I haven't seen anything regarding changing the colour of the overlay and/or completely customizing the dialogue window that pops up and turning it more into an overlay with no "window".
我看过有关在对话框中创建自定义布局的教程(例如http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application),但我什么也没看到关于更改叠加层的颜色和/或完全自定义弹出的对话窗口并将其更多地变成没有“窗口”的叠加层。
回答by justinl
I've solved this problem and created my own custom popup overlay with a custom coloured semi-transparent overlay background using the following steps:
我已经解决了这个问题,并使用以下步骤创建了我自己的带有自定义彩色半透明叠加背景的自定义弹出叠加:
1 - Create a new xml file in your res/values/ folder and name it styles.xml
1 - 在 res/values/ 文件夹中创建一个新的 xml 文件并将其命名为 styles.xml
2 - Here is where you will define your dialog properties. Here is what mine looks like. If you want to replace the default semi-transparent black overlay that shows over the screen, you have to set windowIsFloating
to false, and modify the background of your layout to be whatever colour you want. Here is my file below that I've used:
2 - 您将在此处定义对话框属性。这是我的样子。如果要替换显示在屏幕上的默认半透明黑色覆盖层,则必须设置windowIsFloating
为 false,并将布局的背景修改为您想要的任何颜色。这是我使用的以下文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@color/transparent_white</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
3 - Back in your java code, when creating the dialog object, use the constructor that passes both the context AND the theme. Eg. myDialog = new Dialog(this, R.style.CustomDialogTheme);
(CustomDialogTheme is the name attribute I specified in the styles.xml from step 2)
3 - 返回您的 Java 代码,在创建对话框对象时,使用传递上下文和主题的构造函数。例如。myDialog = new Dialog(this, R.style.CustomDialogTheme);
(CustomDialogTheme 是我在第 2 步中的 style.xml 中指定的名称属性)
4 - Simply set your dialog objects content view to whatever layout you want your dialog to look like. Eg. myDialog.setContentView(R.layout.my_custom_overlay);
If you want your dialog to appear at the center of the screen, set its root element's android:layout_gravity
to center
4 - 只需将对话框对象内容视图设置为您希望对话框看起来像的任何布局。例如。myDialog.setContentView(R.layout.my_custom_overlay);
如果您希望对话框出现在屏幕中央,请将其根元素设置android:layout_gravity
为center
回答by pmko
This worked great for me, but is missing how to close the dialog. if you have a button in your custom layout to close it, here is how to add the listener and close the dialog window.
这对我很有用,但缺少如何关闭对话框。如果您的自定义布局中有一个按钮可以关闭它,这里是如何添加侦听器并关闭对话框窗口。
final Dialog d = new Dialog(this,R.style.CustomDialogTheme);
d.setContentView(R.layout.custom_dialog);
d.show();
Button close_btn = (Button) d.findViewById(R.id.close_btn);
close_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});