Create a drawable with rounded top corners and a border on bottom

守給你的承諾、 提交于 2020-01-15 09:04:13

问题


This is what I came up with.

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/grey" />

            <padding
                android:bottom="1dp" />
            <corners 
                android:radius="0dp"/> 
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/white" />
            <corners 
                android:radius="1dp" 
                android:bottomRightRadius="0dp" 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="5dp"/> 
        </shape>
    </item>
</layer-list>

This is working however the bottom radius is showing up whatever values I place on it.

Actually in only takes the topLeftRadius to make it looks like this

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


回答1:


I had same problem, try it. it worked for me fine. I just add android:top="10dp" to second item. So, it causes to round just top corners.

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid
                android:color="@color/dialog_title_bar_blue"/>

            <corners
                android:topLeftRadius="10dp"
                android:topRightRadius="10dp"/>
        </shape>
    </item>
    <item
        android:top="10dp">
        <shape android:shape="rectangle">
            <solid
                android:color="@color/dialog_title_bar_blue"/>
        </shape>
    </item>
</layer-list>



回答2:


Hi here is your solution "Cheers :) "

first add this layout as background. Then in the below add a view as aline.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >

    <!-- you can use any color you want I used here gray color -->
    <solid android:color="#ffffff" />

    <corners
        android:topLeftRadius="3dp"
        android:topRightRadius="3dp" />

</shape>

And the view

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/black"/>



回答3:


Issue: there is a bug as mentioned here

Solution: the bug has been solved in api12.

so give it a try by providing appropriate resources.create two xml files with same name and put one of them into drawable folder and another into drawable-v12 folder.

-> In drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/grey" />

        <padding
            android:bottom="1dp" />
        <corners 
            android:radius="0dp"/> 
    </shape>
</item>
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/white" />
        <corners 
            android:radius="1dp"
            android:bottomRightRadius="0dp" 
            android:bottomLeftRadius="0dp" 
            android:topLeftRadius="5dp" 
            android:topRightRadius="5dp"/> 
    </shape>
</item>
</layer-list>

-> In drawable-v12 folder:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/grey" />

        <padding
            android:bottom="1dp" />
        <corners 
            android:radius="0dp"/> 
    </shape>
</item>
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/white" />
        <corners 
            android:bottomRightRadius="0dp" 
            android:bottomLeftRadius="0dp" 
            android:topLeftRadius="5dp" 
            android:topRightRadius="5dp"/> 
    </shape>
</item>
</layer-list>

I hope it will be helpful !




回答4:


<item>
    <shape>
        <corners
            android:bottomLeftRadius="0.1dp"
            android:bottomRightRadius="0.1dp"
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp" />

        <solid android:color="#800000" />

        <stroke
            android:width="3dp"
            android:color="@android:color/black" />
    </shape>
</item>
<item android:bottom="3dp">
    <shape>
        <corners
            android:bottomLeftRadius="0.1dp"
            android:bottomRightRadius="0.1dp"
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp" />

        <solid android:color="#800000" />
    </shape>
</item>



来源:https://stackoverflow.com/questions/19042670/create-a-drawable-with-rounded-top-corners-and-a-border-on-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!