【问题标题】:Android Dialog - Custom background instead of dimming or blurringAndroid 对话框 - 自定义背景而不是变暗或模糊
【发布时间】:2014-01-18 07:18:36
【问题描述】:

我创建了自己的自定义对话框,它工作正常,但我想将暗色背景更改为自定义模式(例如图像文件或 xml 形状)。我怎样才能做到这一点?
请注意,我不想改变调光的强度,但我只想用一种模式替换这种调光

【问题讨论】:

  • 你能发布图片来展示你想要实现的目标吗?
  • @GopalRao 我找到了解决我的问题的方法(我已将其作为答案发布)但我担心它的效率,它好吗?如果没有,你能给我一个更好的方法吗?

标签: android android-dialog


【解决方案1】:

我找到了解决这个问题的方法,我从@vipul mittal 的答案中得出这个,
我应该将对话框主题设置如下:

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

有了这个主题,我的对话将是:

  • 全屏因为windowIsFloating设置为false
  • 周围区域完全透明,因为windowBackground 设置为@android:color/transparent

现在我应该用一个扮演周边区域角色的包装器来包装我的对话框 xml 布局内容,在这种情况下,我为此选择了FrameLayout

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

这是我最终对话的截图:

【讨论】:

  • 嗯...很好。我有类似的想法:-)
【解决方案2】:

将对话框主题更改为:

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

在自定义对话框中调用 super 时调用:

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

【讨论】:

  • 感谢您的回复,但这会改变对话框的背景而不是对话框的周围区域
  • 试试这将改变对话框的背景。只需将 android:windowBackground 更改为您想要的
  • 我不想更改对话框背景。相反,我想改变周边地区的格局。此外,您已将 android:windowIsFloating 设置为 false 以拉伸对话框以适应整个屏幕,而我不想要全屏对话框
  • 将对话框的高度和宽度设置为wrap_content,它不会填满整个屏幕。
猜你喜欢
  • 1970-01-01
  • 2011-03-14
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多