Android drawable文件中设置圆角的方法

当我们想为一个图标按钮设置一个圆角背景的时候,我们会自然的想到用一个drawable文件实现这个功能,这里就简单说明一下这个的基础做法。

首先我们要在我们的res文件夹下的drawable文件夹下新建一个文件,我们可以把它命名为bg_corner.xml。文件内容如下:

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

这样我们得到的是一个四个角均为圆角的背景图。如果我们只需要其中的某几个角是圆角,或者圆角大小不同,我们可以用如下写法:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"/>
</shape>

这样我们就完成了drawable文件的编写。之后我们只要将这个xml文件以background的方式设置给按钮或者其他对应要设置的控件即可。

猜你喜欢

转载自blog.csdn.net/weixin_38322371/article/details/90056271