android: shape corners do not work when setting individual corners

后端 未结 5 1557
庸人自扰
庸人自扰 2021-01-02 01:21

I need to have a background which has rounded bottom left/right coners(but not top left/right ones), below is my xml file:



        
5条回答
  •  时光取名叫无心
    2021-01-02 01:25

    The above solutions didn't work for me, but I found a solution online that did work: (https://medium.com/@iamsadesh/android-ui-creating-a-layout-rounded-only-in-the-top-d60514ccab77)

    This is for rounding just the top corners:

    val image = findViewById(R.id.image)
    val curveRadius = 20F
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    
        image.outlineProvider = object : ViewOutlineProvider() {
    
            @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
            override fun getOutline(view: View?, outline: Outline?) {
                outline?.setRoundRect(0, 0, view!!.width, (view.height+curveRadius).toInt(), curveRadius)
            }
        }
    
        image.clipToOutline = true
    
    }
    

提交回复
热议问题