Android 自定义形状对话框

发布于 2025-01-07 03:24:24 字数 194 浏览 0 评论 0原文

我正在尝试在 android 中创建一个自定义形状的对话框。我想要的不是它是矩形的,而是具有我可能想要创建的任何形状。就像放置一个自定义背景,它是一个圆形的 png 图像。

如果我这样做,圆圈外的区域将被白色填充以填充对话框的矩形。我需要的是只隐藏圆圈和布局的其余部分。希望这是有道理的。

据我所知这是不可能的,但也许有人有一些好主意?谢谢。

I am trying to create a custom shaped dialog in android. What I want is instead of it being rectangularly shaped, to have any shape I might want to create. Like put a custom background which is a png image in the shape of a circle.

If I do this, the area outside the circle gets filled with white to fill the rectangle of the dialog. What I need is to have only the circle and the rest of the layout to be hidden. Hope this makes sense.

From what I know this is not possible but still maybe someone has some good ideas ? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2025-01-14 03:24:24

解决这个问题的方法是使用具有透明背景颜色(ARGB #00000000 或 Color.Transparent)的自定义对话框。之后,将线性布局添加到自定义对话框中,并使用 XML 可绘制背景作为背景。在该 XML 中,指定边框半径以使布局成为圆形。接下来,向该线性布局添加另一个布局,将宽度和高度设置为 FILL_PARENT,将背景设置为圆形图像。最后将其余的对话组件添加到第二个布局中。

我记得过去实现过这种效果,但手头没有代码来查看确切的语法。

The way to go around it is to have a custom dialog with a transparent background colour (ARGB #00000000 or Color.Transparent). After that, add a linear layout to your custom dialog with an XML drawable for a background. In that XML, specify border radius to make the layout a circle. Next, add another layout to that linear layout with both width and height set to FILL_PARENT and background set to your circular image. Finally add the rest of your dialogue components to this second layout.

I remember achieving this effect in the past, but don't have the code handy to see the exact syntax.

末骤雨初歇 2025-01-14 03:24:24

好吧,我实际上找到了我想要的:

final Dialog d = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        d.setContentView(R.layout.custom);
        d.setCanceledOnTouchOutside(true);
        d.setCancelable(true);
        return d;

对话框构造函数让我将其设置为透明,然后我可以在布局中做任何我想做的事情,背景是具有任何我想要的形状的 png 图像。不需要 Android 形状或边框。只要您根据对话框设计管理布局,这可以轻松涵盖任何形状,而不仅仅是圆形/矩形等。

Well, I actually found what I want:

final Dialog d = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        d.setContentView(R.layout.custom);
        d.setCanceledOnTouchOutside(true);
        d.setCancelable(true);
        return d;

The dialog constructor let me put it transparent and then I could do anything that I want in my layout with the background a png image with any shape I want. No android shapes or borders needed. This covers easily any shape not just circles/rect etc. as long as you manage your layout accordingly to your dialog design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文