如何在 android 中创建一个圆形 imageButton?

how to create a circular imageButton in android?

伙计们,我在我的布局文件中创建了一个 imageButton 并设置了一个圆形 png 图像作为它的 background.but 当我 运行 我的应用程序时,它显示一个方形按钮并放置了我给定的图像在中间。

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/sliderr" />

添加android:background="@null"

尝试添加这个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="8dip"
    android:bottomLeftRadius="8dip"  
    android:topRightRadius="8dip"
    android:topLeftRadius="8dip"/>
</shape>

res/drawable/round_button.xml 中制作新形状 XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <gradient 
        android:startColor="#FFFF0000" 
        android:endColor="#80FF00FF"
        android:angle="270" />
</shape>

使用这个形状作为按钮的背景:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/round_button"
    android:src="@drawable/sliderr" />

您可能还想添加 android:scaleType="fitCenter" 到您的按钮以使图像与按钮大小相同,并且 android:adjustViewBounds="true" 如果您的图像不相等 height/width.