如何在 Android 中创建完全自定义的 Dialogue/Popup(更改覆盖颜色和对话窗口布局) [英] How to create a completely custom Dialogue/Popup in Android (change overlay colour and dialogue window layout)

查看:31
本文介绍了如何在 Android 中创建完全自定义的 Dialogue/Popup(更改覆盖颜色和对话窗口布局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完全重新设计 Android 中的默认对话组件.具体来说,我想这样做:

I would like to completely re-skin the default dialogue component in Android. Specifically I would like to do this:

  • 将半透明叠加背景从默认的黑色更改为半透明的白色.

  • Change the semi-transparent overlay background from the default black to a semi-transparent white.

改变对话窗口删除默认的窗口框架边框,并用布局替换它在 XML 中定义(它将是浮动的无边界图形纽扣.没有实际的框架.)

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.)

我看过有关在对话框中创建自定义布局的教程(例如 http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application),但我还没有看到任何关于改变颜色覆盖和/或完全自定义弹出的对话窗口,并将其更多地变成没有窗口"的覆盖.

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".

推荐答案

我已经解决了这个问题,并使用以下步骤创建了我自己的自定义弹出式覆盖,并使用自定义彩色半透明覆盖背景:

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 - 在您的 res/values/文件夹中创建一个新的 xml 文件并将其命名为 styles.xml

1 - Create a new xml file in your res/values/ folder and name it styles.xml

2 - 您将在此处定义对话框属性.这是我的样子.如果要替换显示在屏幕上的默认半透明黑色叠加层,则必须将 windowIsFloating 设置为 false,并将布局的背景修改为您想要的任何颜色.下面是我使用过的文件:

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:

<?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 - 回到您的 java 代码中,创建对话框对象时,使用传递上下文和主题的构造函数.例如.myDialog = new Dialog(this, R.style.CustomDialogTheme);(CustomDialogTheme 是我在步骤 2 中的 styles.xml 中指定的名称属性)

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)

4 - 只需将对话框对象内容视图设置为您希望对话框看起来像的任何布局.例如.myDialog.setContentView(R.layout.my_custom_overlay);如果您希望对话框出现在屏幕中央,请将其根元素的 android:layout_gravity 设置为 center

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

这篇关于如何在 Android 中创建完全自定义的 Dialogue/Popup(更改覆盖颜色和对话窗口布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆