我想制作一个圆形按钮,但是我不知道如何做到。我能够制作带有圆角的按钮,但是如何让它变成圆形呢?这不是一样的。请问,在Android中是否可能实现?谢谢。
我想制作一个圆形按钮,但是我不知道如何做到。我能够制作带有圆角的按钮,但是如何让它变成圆形呢?这不是一样的。请问,在Android中是否可能实现?谢谢。
在drawable文件夹中创建名为roundedbutton.xml
的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>
最后将它设置为您的 Button
的背景,使用 android:background = "@drawable/roundedbutton"
如果您想完全圆形化它,请更改半径并选择适合您的尺寸。
如果你使用Android Studio,你只需要使用:
<?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>
这对我来说运作良好,希望能帮助到别人。
创建一个包含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>
在任何布局文件中将其用于按钮标签
<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"
/>
<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" />
结果:
如果您更改按钮的大小,请注意使用按钮大小的一半作为app:cornerRadius
。
<?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>
2021年更新:
只需使用MaterialButton即可。
<com.google.android.material.button.MaterialButton
app:cornerRadius="30dp"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="test" />
implementation 'com.google.android.material:material:1.6.1'
添加到 build.gradle(:app) 中。 - daksh<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 -->
android:layout_width="50dp"
android:layout_height="50dp"
使用椭圆形作为形状。这将使按钮成为椭圆形。
<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>
MaterialButton
: <com.google.android.material.button.MaterialButton
android:layout_width="48dp"
android:layout_height="48dp"
android:insetTop="0dp"
android:insetBottom="0dp"
android:text="A"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.Rounded"
/>
并应用一个带有以下内容的圆形ShapeAppearanceOverlay
:
<style name="ShapeAppearanceOverlay.App.rounded" parent="">
<item name="cornerSize">50%</item>
</style>