Android Dialog - Custom background instead of dimming or blurring

我是研究僧i 提交于 2019-12-07 04:48:29

问题


I have created my own custom dialog and it works fine but I want to change dimmed background to a custom pattern (for example an image file or a xml shape). How can I achieve that?
Note that I do not want to change intensity of dimming but I just want, this dimming be replaced with a pattern


回答1:


I found a workaround for this problem, I derived this from @vipul mittal answer,
I should set dialog theme as following:

<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>

With this theme my dialog will be:

  • full screen because windowIsFloating is set to false
  • surrounding area is fully transparent because windowBackground is set to @android:color/transparent

Now I should wrap my dialog xml layout contents with a wrapper that plays surrounding area role, in this case I have picked FrameLayout for this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/dialog_back"> <!-- this is surrounding area drawable -->

    <!-- dialog contents goes here -->

</FrameLayout>

Here is a screenshot of my final dialog:




回答2:


Change dialog theme to:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

In custom dialog when you call super call this:

public CustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
}


来源:https://stackoverflow.com/questions/21201272/android-dialog-custom-background-instead-of-dimming-or-blurring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!