带有XML的ImageButton背景

时间:2014-12-25 09:09:20

标签: android xml background imageview imagebutton

我正在尝试制作圆形的Android ImageButton。为此,我根据link编写以下代码。

在我的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>

在我的res / drawable / roundcorner.xml

<?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>

但代码不起作用。输出如下 Image1

但是,当我在 main.xml

中将 ImageButton 更改为 ImageView
<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"  />

它的工作原理如下

Image2

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

1 个答案:

答案 0 :(得分:0)

让你的src成为android:src =“@ drawable / roundcorner”,或者让它透明,这样你就可以看到背后的圆形背景。