android: shape corners do not work when setting individual corners

后端 未结 5 1556
庸人自扰
庸人自扰 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<ImageView>(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
    
    }
    
    0 讨论(0)
  • 2021-01-02 01:30

    Change this:

     <corners 
            android:bottomRightRadius="12dp" 
            android:bottomLeftRadius="12dp"
            android:topLeftRadius="0dp" 
            android:topRightRadius="0dp"/>
    

    to this:

     <corners 
            android:radius="1dp"
            android:bottomRightRadius="12dp" 
            android:bottomLeftRadius="12dp"
            android:topLeftRadius="0dp" 
            android:topRightRadius="0dp"/>
    

    and it should be working as expected.

    0 讨论(0)
  • 2021-01-02 01:31

    I found there may be a bug that if you set individual corners, and if any of them is 0, all of them become 0, so in the end I set two of them to 1dip and other two to whatever I need, as none of them is 0, so the bug does not affect it and the result looks good.

    0 讨论(0)
  • 2021-01-02 01:41

    It seems to be a known issue. Every corner must be >1 or else no corners will be rounded. According to the Android documentation it can be done but it's kind of hacky:

    Note: Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use android:radius to set a default corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want >rounded corners.

    See here: http://developer.android.com/guide/topics/resources/drawable-resource.html#corners-element

    0 讨论(0)
  • 2021-01-02 01:44

    try this its work for me.

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" android:padding="10dp">
            <solid android:color="#FFFFFF"/>
            <corners 
                android:bottomRightRadius="30dp" 
                android:bottomLeftRadius="30dp"
                android:topLeftRadius="30dp" 
                android:topRightRadius="30dp"/>
    </shape>
    
    0 讨论(0)
提交回复
热议问题