简体   繁体   中英

Android make view fill it's background shape

Question:

I have got an Activity used as a popup.

It's background is a shape with rounded corners.

But the items of it's content view don't fill out the shape. They are more likely inside of an invisible rectangle inside of the shape.

Can I somehow make them fill the shape and being cut when overlapping the corners?


Solution:

  1. Add this code to the view with the shape background:

view.setClipToOutline(true); --> clips the view view.setOutlineProvider(ViewOutlineProvider.BACKGROUND); --> tells the view to be clipped by the background(shape)

  1. If the paddings of the view are too big, just make them smaller so you can see an effect of the clipping

Yes, it's possible to do this. It's called View Clipping .

Here is an existing question about clipping: Android View Clipping

You have 2 options:

1- Implement a custom View and override onDraw so you can perform the clipping manually. See here: https://stackoverflow.com/a/28206555/6007104

2- Use View.outlineProvider and View.clipToOutline . This is the easier option, but only available on SDK 21+. See here: https://stackoverflow.com/a/54202660/6007104

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