如何制作圆形按钮?

时间:2017-05-15 05:56:03

标签: android xml shape

我是Android开发的新手。在使用Android Studio 2.2时,我尝试创建一个循环按钮。从here开始,我尝试创建一个新的可绘制资源文件。但我无法将根元素更改为形状。没有这样的选择。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:2)

有两种解决方案。 浮动操作按钮:https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html

否则: 您在xml中创建一个椭圆形状。 并将该形状作为背景放在按钮上,并确保按钮具有相同的layout_height和layout_width。

实施例: oval_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:shape="oval">

    <solid android:color="@color/colorAccent"/>

</shape>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

  <Button
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:background="@drawable/oval_shape"/>



</FrameLayout>

答案 1 :(得分:0)

此处不需要任何选项。只需使用<shape>
检查此示例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary" />
    <corners android:radius="@dimen/btn_radius" />
</shape>

答案 2 :(得分:0)

像这样使用xml drawable:

将以下内容保存为drawable文件夹中的round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
    <shape android:shape="oval">
        <solid android:color="#fa09ad"/>
    </shape>
</item>
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="#c20586"/>
    </shape>
</item>

并将其设置为xml中Button的背景,如下所示:

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="hello"
android:textColor="#fff" />

答案 3 :(得分:0)

您可能正在寻找FloatingActionButton 它在支持库中提供。 班级的documation