我想创建自定义按钮,我需要它是圆。我如何创建一个圆形按钮? 我认为使用draw9patch是不可能的。
我也不知道如何自定义按钮!
你有什么建议吗?
我想创建自定义按钮,我需要它是圆。我如何创建一个圆形按钮? 我认为使用draw9patch是不可能的。
我也不知道如何自定义按钮!
你有什么建议吗?
当前回答
下面是如何简单地执行,在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
其他回答
使用xml绘制像这样:
将以下内容保存为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>
</selector>
Android材质效果:虽然FloatingActionButton是一个更好的选择,如果你想使用xml选择器,创建一个文件夹drawable-v21在res和保存另一个round_button.xml下面的xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#c20586">
<item>
<shape android:shape="oval">
<solid android:color="#fa09ad"/>
</shape>
</item>
</ripple>
在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" />
重要的是:
如果你想要它显示所有这些状态(启用,禁用,高亮等),你将使用这里描述的选择器。 为了使可绘制文件向后兼容,您必须同时保留两个文件。否则,在以前的android版本中,你会遇到奇怪的异常。
Markushi写了一个具有惊人效果的圆圈按钮小部件。点击这里!
AngryTool自定义android按钮
你可以用这个工具网站制作任何类型的自定义android按钮… 我使圆形和方形按钮与圆角与这个工具站点.. 也许我能帮助你
如果你想使用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>
在官方的材质组件库中,你可以使用MaterialButton来应用Widget.MaterialComponents.Button.Icon样式。
喜欢的东西:
<com.google.android.material.button.MaterialButton
android:layout_width="48dp"
android:layout_height="48dp"
style="@style/Widget.MaterialComponents.Button.Icon"
app:icon="@drawable/ic_add"
app:iconSize="24dp"
app:iconPadding="0dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
/>
目前,app:iconPadding="0dp",android:insetLeft,android:insetTop,android:insetRight,android:insetBottom属性需要在按钮上的图标中心避免额外的填充空间。
使用app:shapeAppearanceOverlay属性来获得圆角。在这种情况下,你会得到一个圆。
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
最终结果:
与喷气背包撰写你可以使用:
Button(
onClick = { /* Do something! */ },
modifier = Modifier.width(48.dp).height(48.dp),
shape = CircleShape
) {
Icon(Icons.Filled.Add, "")
}