如何在 android 中创建一个带有背景图像的圆形按钮?

How to create a circular button in android with image in the background?

我想在 android 中创建一个圆形按钮。这是我正在使用的 xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9F2200"/>
<stroke android:width="2dp" android:color="#fff" />
</shape>

现在当我在布局中放置一个按钮时,我必须将上面的 xml 设置为

android:background

属性。但是我怎样才能将图像放在我生成的圆形按钮中。如何在 xml 本身内完成此操作?

如果我知道了,你可以使用 ImageButton,它可以让你选择 android:background 的背景和 android:src 的可绘制资源。

使用 ImageButton 并使用 src 标签设置图像:android:src="@drawable/your_image"

您可以使用 FrameLayout

<FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:background="your selector" >

            <Button
                android:id="@+id/fbLogin"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="yourimage"
                android:gravity="center"/>
        </FrameLayout>