我想创建自定义按钮,我需要它是圆。我如何创建一个圆形按钮? 我认为使用draw9patch是不可能的。
我也不知道如何自定义按钮!
你有什么建议吗?
我想创建自定义按钮,我需要它是圆。我如何创建一个圆形按钮? 我认为使用draw9patch是不可能的。
我也不知道如何自定义按钮!
你有什么建议吗?
当前回答
对于MaterialButton上的FAB样式的按钮:
<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。
其他回答
对于MaterialButton上的FAB样式的按钮:
<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。
AngryTool自定义android按钮
你可以用这个工具网站制作任何类型的自定义android按钮… 我使圆形和方形按钮与圆角与这个工具站点.. 也许我能帮助你
下面是如何简单地执行,在drawable.xml中创建一个可绘制的资源文件。输入round_button.xml,然后粘贴下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid
android:color="@color/button_start_gradient_color"/>
</shape>
</item>
<item
android:drawable="@drawable/microphone"/>
</layer-list>
注意:-使用你自己的颜色和可绘制资源,因为我已经使用@drawable/麦克风
以下是结果 [1]: https://i.stack.imgur.com/QyhdJ.png
如果你想使用VectorDrawable和ConstraintLayout
<FrameLayout
android:id="@+id/ok_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:background="@drawable/circle_button">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/icon_of_button"
android:layout_width="32dp"
android:layout_height="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="@drawable/ic_thumbs_up"/>
<TextView
android:id="@+id/text_of_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/icon_of_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="5dp"
android:textColor="@android:color/white"
android:text="ok"
/>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
圆圈背景:circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
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>
如果你想使用ImageButton,请使用下面的方法。它将创建带有材质波纹的圆形ImageButton。
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_settings_6"
android:background="?selectableItemBackgroundBorderless"
android:padding="10dp"
/>