如何在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:29:58  来源:igfitidea点击:

How to get round shape in Android

androiddrawableshape

提问by VishalSoni

How would I achieve a round shape in Android, like below, through an Android shape drawable:

enter image description here

我将如何通过 Android 形状可绘制在 Android 中实现圆形,如下所示:

在此处输入图片说明

回答by Graham Smith

You need to create a shape drawablein 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

编辑 - 代码结果的屏幕截图

Graphical View

图形视图

Code Image

代码图片