【问题标题】:ImageButton background with XML带有 XML 的 ImageButton 背景
【发布时间】:2014-12-25 09:09:19
【问题描述】:

我正在尝试制作一个圆形的 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>

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

但是,当我在 ma​​in.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"  />

如下所示

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

【问题讨论】:

  • 你能发布完整的 xml...
  • 我已经更新了完整的 xml

标签: android xml background imageview imagebutton


【解决方案1】:

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

【讨论】:

  • 结果类似于this。所以看起来大图遮住了按钮……有没有办法让图像适合圆形按钮(裁剪图像以适合圆形按钮)?
  • 如果您想要与 imageview 输出相同的内容,请选中此项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2012-07-24
相关资源
最近更新 更多