gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:44:28 26 4
gpt4 key购买 nike

在设置 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com