如何拥有透明的ImageButton:Android

发布于 2024-09-12 23:00:59 字数 363 浏览 5 评论 0原文

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@drawable/transparent"></ImageButton>

这就是我尝试获取透明 ImageButton 的方法,以便将这些按钮放置在 SurfaceView 上。但是,当我在 xml 中包含透明行时,Eclipse 就会在项目中出现错误。

请帮忙。

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@drawable/transparent"></ImageButton>

This is what I tried to get a transparent ImageButton so as to place those buttons on a SurfaceView. But Eclipse, gives me an error in the project as soon as I include the transparent line in xml.

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

豆芽 2024-09-19 23:00:59

尝试使用 null 作为背景...

android:background="@null"

Try using null for the background ...

android:background="@null"
彩扇题诗 2024-09-19 23:00:59

不要使用透明或空布局,因为这样按钮(或通用视图)将不再在点击时突出显示!

我也遇到了同样的问题,最后我从 Android API 中找到了正确的属性来解决问题。它可以应用于任何视图。

在按钮规格中使用它:

android:background="?android:selectableItemBackground"

DON'T USE A TRANSAPENT OR NULL LAYOUT because then the button (or the generic view) will no more highlight at click!!!

I had the same problem and finally I found the correct attribute from Android API to solve the problem. It can apply to any view.

Use this in the button specifications:

android:background="?android:selectableItemBackground"
杀手六號 2024-09-19 23:00:59

您还可以使用透明颜色:

android:background="@android:color/transparent"

You can also use a transparent color:

android:background="@android:color/transparent"
放肆 2024-09-19 23:00:59

将背景设置为“@null”将使按钮在单击时没有任何效果。这将是一个更好的选择。

style="?android:attr/borderlessButtonStyle"

后来发现使用

android:background="?android:attr/selectableItemBackground"

也是一个不错的解决方案。并且您可以按照自己的风格继承此属性。

Setting the background to "@null" will make the button have no effect when clicked. This will be a better choice.

style="?android:attr/borderlessButtonStyle"

Later I found that using

android:background="?android:attr/selectableItemBackground"

is also a good solution. And you can inherit this attribute in your own style.

岁月苍老的讽刺 2024-09-19 23:00:59

在运行时,您可以使用以下代码

btn.setBackgroundDrawable(null);

in run time, you can use following code

btn.setBackgroundDrawable(null);
っ左 2024-09-19 23:00:59

如果您需要点击动画,请不要使用 null 或透明。更好的:

//Rectangular click animation
android:background="?attr/selectableItemBackground"

//Rounded click animation
android:background="?attr/selectableItemBackgroundBorderless"

Don't use null or transparent if you need a click animation. Better:

//Rectangular click animation
android:background="?attr/selectableItemBackground"

//Rounded click animation
android:background="?attr/selectableItemBackgroundBorderless"
一场信仰旅途 2024-09-19 23:00:59

我相信公认的答案应该是:
<代码>
android:background="?attr/selectableItemBackground"

这与 @lory105 的答案相同,但它使用支持库来实现最大兼容性(android: 等效项仅适用于 API >= 11)

I believe the accepted answer should be:

android:background="?attr/selectableItemBackground"

This is the same as @lory105's answer but it uses the support library for maximum compatibility (the android: equivalent is only available for API >= 11)

倾城°AllureLove 2024-09-19 23:00:59

删除这一行:

android:background="@drawable/transparent">

在您的活动类集中,

ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);

您可以将 alpha 级别设置为 0 到 255

o 表示透明,255 表示不透明。

Remove this line :

android:background="@drawable/transparent">

And in your activity class set

ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);

You can set alpha level 0 to 255

o means transparent and 255 means opaque.

幽梦紫曦~ 2024-09-19 23:00:59

最好的方法是使用透明颜色代码

android:background="#00000000"

使用颜色代码#00000000使任何东西透明

The best way is using the transparent color code

android:background="#00000000"

use the color code #00000000 for making any thing transparent

俏︾媚 2024-09-19 23:00:59

使用这个:

<ImageButton
 android:id="@+id/back"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@null"
 android:padding="10dp"
 android:src="@drawable/backbtn" />

Use this:

<ImageButton
 android:id="@+id/back"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@null"
 android:padding="10dp"
 android:src="@drawable/backbtn" />
尐偏执 2024-09-19 23:00:59

使用ImageView...它默认具有透明背景...

Use ImageView... it have transparent background by default...

不寐倦长更 2024-09-19 23:00:59

我已经在背景中添加了一些东西,所以,这个东西对我有用:

   android:backgroundTint="@android:color/transparent"

(Android Studio 3.4.1)

编辑:仅适用于 android api 级别 21 及以上。为了兼容性,请改用它

   android:background="@android:color/transparent"

I was already adding something to the background so , This thing worked for me:

   android:backgroundTint="@android:color/transparent"

(Android Studio 3.4.1)

EDIT: only works on android api level 21 and above. for compatibility, use this instead

   android:background="@android:color/transparent"
惟欲睡 2024-09-19 23:00:59

您可以使用以下代码通过将背景设置为透明来正常工作:

<ImageButton 
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="transparent"></ImageButton>

You can use the following code works just fine by setting the background to transparent:

<ImageButton 
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="transparent"></ImageButton>
╰沐子 2024-09-19 23:00:59

在 XML 中将 ImageButton 的背景设置为 @null

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@null"></ImageButton>

Set the background of the ImageButton as @null in XML

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@null"></ImageButton>
初懵 2024-09-19 23:00:59

使用 "@null" 。这对我有用。

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/bkash"
    android:id="@+id/bid1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@null" />

Use "@null" . It worked for me.

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/bkash"
    android:id="@+id/bid1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@null" />
醉南桥 2024-09-19 23:00:59

它是 android:background="@android:color/transparent"

<ImageButton
    android:id="@+id/imageButton"
    android:src="@android:drawable/ic_menu_delete"
    android:background="@android:color/transparent"
/>

It's android:background="@android:color/transparent"

<ImageButton
    android:id="@+id/imageButton"
    android:src="@android:drawable/ic_menu_delete"
    android:background="@android:color/transparent"
/>
烟花易冷人易散 2024-09-19 23:00:59

它可以工作并且还可以保留单击按钮的视觉反馈,

android:backgroundTintMode="screen"

It works and also keeps visual feedback for button clicked,

android:backgroundTintMode="screen"
十年九夏 2024-09-19 23:00:59

您可以使用: android:background="#00000000"android:backgroundTint="#00000000"

you can either use: android:background="#00000000" or android:backgroundTint="#00000000"

所谓喜欢 2024-09-19 23:00:59

这是以编程方式将背景颜色设置为透明

 ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
 btn.setBackgroundColor(Color.TRANSPARENT);

This is programatically set background color as transparent

 ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
 btn.setBackgroundColor(Color.TRANSPARENT);
夏尔 2024-09-19 23:00:59

以编程方式可以通过以下方式完成:

image_button.setAlpha(0f) // to make it full transparent
image_button.setAlpha(0.5f) // to make it half transparent
image_button.setAlpha(0.6f) // to make it (40%) transparent
image_button.setAlpha(1f) // to make it opaque

Programmatically it can be done by :

image_button.setAlpha(0f) // to make it full transparent
image_button.setAlpha(0.5f) // to make it half transparent
image_button.setAlpha(0.6f) // to make it (40%) transparent
image_button.setAlpha(1f) // to make it opaque
往昔成烟 2024-09-19 23:00:59

如果您想在 .xml 中执行此操作,请使用以下代码:

android:background="@null"

并且,这是以编程方式执行此操作的示例

yourButton.setBackgroundResource(0);

If you want to do it in a .xml use the below code:

android:background="@null"

And, here is an example of doing it programmatically

yourButton.setBackgroundResource(0);
束缚m 2024-09-19 23:00:59

不要忘记添加填充,它有助于显示按钮效果。添加内边距以根据此背景进行调整。

android:background="?android:selectableItemBackground"
android:padding="5dp"

Dont forget to add padding, it helps to show button effect. Add padding to adjust with this background.

android:background="?android:selectableItemBackground"
android:padding="5dp"
黯淡〆 2024-09-19 23:00:59
<ImageButton
    android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/media_skip_backward">
</ImageButton>

我为 ImageButton 使用了透明的 png,并且 ImageButton 工作了。

<ImageButton
    android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/media_skip_backward">
</ImageButton>

I used a transparent png for the ImageButton, and the ImageButton worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文