Android 带有XML的ImageButton背景

Android 带有XML的ImageButton背景,android,xml,background,imageview,imagebutton,Android,Xml,Background,Imageview,Imagebutton,我正在试着做一个圆形的按钮。为此,我根据下面的代码编写了以下代码 在my main.xml中 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"

我正在试着做一个圆形的按钮。为此,我根据下面的代码编写了以下代码

在my main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:textColor="#333"
        android:textSize="18sp"
        android:text="Invite Activity."  />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:src="@drawable/round"
        android:background="@drawable/roundcorner"
        android:padding="50dp"  />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#33DDFF" />
    <corners android:radius="100dp" />
</shape>
<ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/imageButton"
     android:src="@drawable/round"
     android:background="@drawable/roundcorner"
     android:padding="50dp"  />

在my res/drawable/roundcorner.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:textColor="#333"
        android:textSize="18sp"
        android:text="Invite Activity."  />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:src="@drawable/round"
        android:background="@drawable/roundcorner"
        android:padding="50dp"  />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#33DDFF" />
    <corners android:radius="100dp" />
</shape>
<ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/imageButton"
     android:src="@drawable/round"
     android:background="@drawable/roundcorner"
     android:padding="50dp"  />

但是代码不起作用。输出如下所示

但是,当我将main.xml中的ImageButton更改为ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:textColor="#333"
        android:textSize="18sp"
        android:text="Invite Activity."  />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:src="@drawable/round"
        android:background="@drawable/roundcorner"
        android:padding="50dp"  />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#33DDFF" />
    <corners android:radius="100dp" />
</shape>
<ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/imageButton"
     android:src="@drawable/round"
     android:background="@drawable/roundcorner"
     android:padding="50dp"  />

它的工作原理如下


我很困惑,因为我知道ImageButton继承自ImageView。为什么它的工作方式不同?有没有办法解决这个问题?提前谢谢

将您的src设置为android:src=“@drawable/roundcorner”,或者将其设置为透明,以便您可以看到其背后的圆形背景。

您可以发布完整的xml吗……我已经更新了完整的xml。结果如下所示。所以看起来大图像掩盖了按钮。。。是否有方法使图像适合圆形按钮(裁剪图像以适合圆形按钮)?如果您想要与imageview输出相同,请选中此选项。