Android 如何使用 xml 设置可绘制颜色的圆角半径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2122199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set the rounded corner radius of a color drawable using xml?
提问by Jay Askren
On the android website, there is a section about color drawables. Defining these drawables in xml looks like this:
在 android 网站上,有一个关于 color drawables的部分。在 xml 中定义这些可绘制对象如下所示:
<resources>
<drawable name="solid_red">#f00</drawable>
<drawable name="solid_blue">#0000ff</drawable>
<drawable name="solid_green">#f0f0</drawable>
</resources>
In the java api, they have thr following method to define rounded corners:
在java api中,他们有以下方法来定义圆角:
setCornerRadius(float radius)
Is there a way to set the rounded corners in the xml?
有没有办法在xml中设置圆角?
回答by Mark B
Use the <shape>
tag to create a drawable in XML with rounded corners. (You can do other stuff with the shape tag like define a color gradient as well).
使用该<shape>
标签在 XML 中创建一个带有圆角的 drawable。(你可以用形状标签做其他事情,比如定义颜色渐变)。
Here's a copy of a XML file I'm using in one of my apps to create a drawable with a white background, black border and rounded corners:
这是我在我的一个应用程序中使用的 XML 文件的副本,用于创建具有白色背景、黑色边框和圆角的 drawable:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp"
android:color="#ff000000" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
回答by John Pisello
mbaird's answer works fine. Just be aware that there seems to be a bug in Android (2.1 at least), that if you set any individual corner's radius to 0, it forces all the corners to 0 (at least that's the case with "dp" units; I didn't try it with any other units).
mbaird 的回答工作正常。请注意,Android 中似乎存在一个错误(至少 2.1),如果将任何单个角的半径设置为 0,它会强制所有角为 0(至少“dp”单位就是这种情况;我没有不要与任何其他单位一起尝试)。
I needed a shape where the top corners were rounded and the bottom corners were square. I got achieved this by setting the corners I wanted to be square to a value slightly larger than 0: 0.1dp. This still renders as square corners, but it doesn't force the other corners to be 0 radius.
我需要一个上角为圆形而下角为方形的形状。我通过将我想要方形的角设置为略大于 0 的值来实现这一点:0.1dp。这仍然呈现为方角,但它不会强制其他角为 0 半径。
回答by Mujahid khan
Try below code
试试下面的代码
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<solid android:color="#1271BB" />
<stroke
android:width="5dp"
android:color="#1271BB" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" /></shape>