如何在Android中获得圆形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10103900/
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 to get round shape in Android
提问by VishalSoni
How would I achieve a round shape in Android, like below, through an Android shape drawable:
我将如何通过 Android 形状可绘制在 Android 中实现圆形,如下所示:
回答by Graham Smith
You need to create a shape drawable
in the drawable folder that looks something like:
您需要drawable
在 drawable 文件夹中创建一个形状,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
</shape>
(For this example I have saved the drawable as circle.xml and it will have a gradient fill)
(在这个例子中,我将 drawable 保存为 circle.xml 并且它会有一个渐变填充)
Then in your layout you need to define a view and set the shape as the background:
然后在您的布局中,您需要定义一个视图并将形状设置为背景:
<View android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/circle"/>
The View defines the size and dimensions of the shape.
视图定义了形状的大小和尺寸。
Edit - Screenshots of the result of the code
编辑 - 代码结果的屏幕截图