Android 如何制作圆形按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10266595/
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 03:03:40  来源:igfitidea点击:

How to make a round button?

android

提问by user1166635

I'm trying to make a round button, but I don't know how can I do it. I can make button with rounded corners, but how can I can round circle. It's not the same. Please, tell me, is it possible on Android? Thank you.

我正在尝试制作一个圆形按钮,但我不知道该怎么做。我可以制作带有圆角的按钮,但我怎么能圆圆。这是不一样的。请告诉我,在Android上可以吗?谢谢你。

回答by Arif Nadeem

Create an xml file named roundedbutton.xmlin drawable folder

创建一个roundedbutton.xml在 drawable 文件夹中命名的 xml 文件

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

Finally set that as background to your Buttonas android:background = "@drawable/roundedbutton"

最后设置为背景,以您Buttonandroid:background = "@drawable/roundedbutton"

If you want to make it completely rounded, alter the radius and settle for something that is ok for you.

如果你想让它完全圆润,改变半径并选择适合你的东西。

回答by Shaun Jones

If using Android Studio you can just use:

如果使用 Android Studio,您可以使用:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring">
        <solid android:color="#FFFFFF"/>

    </shape>

this works fine for me, hope this helps someone.

这对我来说很好用,希望这对某人有帮助。

回答by Mohit

  1. Create a drawable/button_states.xml file containing:

    <?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="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="#41ba7a" />
            <stroke
                android:width="2dip"
                android:color="#03ae3c" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
            </shape>
        </item>
        <item android:state_pressed="true"> 
            <shape android:shape="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="#3AA76D" />
            <stroke
                android:width="2dip"
                android:color="#03ae3c" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
            </shape>
        </item>
    </selector>
    
  2. Use it in button tag in any layout file

    <Button
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:background="@drawable/button_states"
        android:text="@string/btn_scan_qr"
        android:id="@+id/btn_scan_qr"
        android:textSize="15dp"
    />
    
  1. 创建一个 drawable/button_states.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="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="#41ba7a" />
            <stroke
                android:width="2dip"
                android:color="#03ae3c" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
            </shape>
        </item>
        <item android:state_pressed="true"> 
            <shape android:shape="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="#3AA76D" />
            <stroke
                android:width="2dip"
                android:color="#03ae3c" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
            </shape>
        </item>
    </selector>
    
  2. 在任何布局文件的按钮标签中使用它

    <Button
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:background="@drawable/button_states"
        android:text="@string/btn_scan_qr"
        android:id="@+id/btn_scan_qr"
        android:textSize="15dp"
    />
    

回答by LOG_TAG

Markushi's android circlebutton:

Markushi 的 android 圆形按钮:

(This library is deprecated and no new development is taking place. Consider using a FAB instead.)

(这个库已被弃用,并且没有进行新的开发。考虑使用 FAB。)

enter image description here

在此处输入图片说明

回答by user4512768

<corners android:bottomRightRadius="180dip"
    android:bottomLeftRadius="180dip"
    android:topRightRadius="180dip"
    android:topLeftRadius="180dip"/>

<solid android:color="#6E6E6E"/> <!-- this one is ths color of the Rounded Button -->

and add this to the button code

并将其添加到按钮代码中

    android:layout_width="50dp"
    android:layout_height="50dp"

回答by Rohit Goyal

Used the shape as oval. This makes the button oval

使用的形状为椭圆形。这使按钮变成椭圆形

<item>
    <shape android:shape="oval" >
        <stroke
            android:height="1.0dip"
            android:width="1.0dip"
            android:color="#ffee82ee" />

        <solid android:color="#ffee82ee" />

        <corners
            android:bottomLeftRadius="12.0dip"
            android:bottomRightRadius="12.0dip"
            android:radius="12.0dip"
            android:topLeftRadius="12.0dip"
            android:topRightRadius="12.0dip" />
    </shape>
</item>

回答by Giovanny Pi?eros

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

Set that on your XML drawable resources, and simple use and image button with an round image, using your drawable as background.

将它设置在您的 XML 可绘制资源上,并使用您的可绘制对象作为背景,简单使用和带有圆形图像的图像按钮。

回答by MAC

use ImageButtoninstead of Button....

使用ImageButton而不是 Button....

and make Round image with transparentbackground

并制作具有透明背景的圆形图像

回答by Roberto Leinardi

If you want a FAB looking circular button and you are using the official Material Component library you can easily do it like this:

如果你想要一个看起来像 FAB 的圆形按钮并且你正在使用官方的 Material Component 库,你可以很容易地这样做:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
    app:cornerRadius="28dp"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:text="1" />

Result:

结果:

enter image description here

在此处输入图片说明

If you change the size of the button, just be careful to use half of the button size as app:cornerRadius.

如果您更改按钮的大小,请注意将按钮大小的一半用作app:cornerRadius.

回答by chavaone

Round button in Android

Android 中的圆形按钮

You can make a ImageButtonwith circular background image.

您可以制作ImageButton带有圆形背景的图像。