具有透明背景的android AlertDialog [英] android AlertDialog with transparent background

查看:84
本文介绍了具有透明背景的android AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的AlertDialog,我想使其背景完全透明. 通常,为了使活动完全透明,我执行以下操作

I have a custom AlertDialog and I want to make it's background completely transparent. Normally to make an activity completely transparent, I do the following

  • 在xml布局中将背景设置为#00000000

.

在onCreate中添加getWindow().setBackgroundDrawable(new ColorDrawable(0)).

In onCreate add getWindow().setBackgroundDrawable(new ColorDrawable(0)).

但是现在我正在处理一个对话框,我该如何实现透明性?

But now that I am dealing with a Dialog, how do I accomplish transparency?

这是对话框代码:

LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.activity_mine1,
        (ViewGroup) findViewById(R.layout.mine1));
    mine1 = new AlertDialog.Builder(this);
    mine1.setView(dialoglayout);
    mine1.show();

我的xml只是带有其他子视图的relativeLayout:

And my xml is just a relativeLayout with other child views:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000" >

    ...

</RelativeLayout>

注意:我已经在这里查看过一些类似的帖子,但是它们似乎不起作用.

Note: I have already looked at some similar posts here, but they don't seem to work.

我的真正原因是我真正想要使用的背景不是矩形.我可以在一项活动中使用它.但是我想改用对话框.

My real reason is that the background that I really want to use, is not rectangular. I get it to work in an activity. But I want to use a dialog instead.

进一步玩耍,我有这个style.xml

Further playing around, I have this style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomDialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
</resources>

我添加为

new AlertDialog.Builder(this, R.style.CustomDialog)

推荐答案

使用对话框代替AlertDialog.Builder,因此使用setContentView代替setView.

Use Dialog instead of AlertDialog.Builder and so use setContentView instead of setView.

这篇关于具有透明背景的android AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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