简体   繁体   中英

Android Custom shaped dialog

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM