android - ImageButton 在背景颜色覆盖时显示奇怪的行为

标签 android android-imagebutton

在设置 ImageButton 时,我注意到它有一些灰色背景颜色,与 UI 的其余部分不协调。

enter image description here

这是 ImageButton 的代码

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_material"/>

但是当我尝试像这样将背景覆盖为透明时:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_material"
    android:background="@android:color/transparent"/>

按钮完全失去了形状和填充

enter image description here

有人知道为什么会这样吗?

最佳答案

imageButton 有背景的原因是因为它基本上是一个带有图像而不是文本的按钮。所以根据 android 文档:

ImageButton public class ImageButton extends ImageView

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the setImageResource(int) method. To remove the standard button background image, define your own background image or set the background color to be transparent. To indicate the different button states (focused, selected, etc.), you can define a different image for each state. E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector." For example:

version="1.0" encoding="utf-8"?>  <selector
> xmlns:android="http://schemas.android.com/apk/res/android">
>      <item android:state_pressed="true"
>            android:drawable="@drawable/button_pressed" /> <!-- pressed -->
>      <item android:state_focused="true"
>            android:drawable="@drawable/button_focused" /> <!-- focused -->
>      <item android:drawable="@drawable/button_normal" /> <!-- default -->  </selector> 

将 XML 文件保存在项目的 res/drawable/文件夹中,然后将其作为源代码的可绘制对象引用

ImageButton (in the android:src attribute). Android will automatically change the image based on the state of the button and the corresponding images defined in the XML. The order of the elements is important because they are evaluated in order. This is why the "normal" button image comes last, because it will only be applied after android:state_pressed and android:state_focused have both evaluated false. See the Buttons guide.

有关更多信息,请查看文档:Image Button

关于android - ImageButton 在背景颜色覆盖时显示奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47572386/

相关文章:

android - 如何在 Jetpack Compose 中使用图像占位符进行预览

android - 是否可以制作一个可以在 Android 中切换 Talkback 的小部件?

java - 无法将函数 "OnClickListener"添加到 ImageButton

android - 在分隔的工具栏中具有onClick的ImageButton

android - 将 ImageButton 的默认背景设置为透明(使用 AndroidX)

android - 更改 Activity 时出现黑屏

android - 使用 FLAG_ACTIVITY_REORDER_TO_FRONT 在持续运行的 UI Activity 之间切换会导致 "no window focus"错误

Android Imagebutton被剪裁

java - Android - findViewById() 和动态设置 ID

android - 如何为图像按钮的图标赋予强调色?