简体   繁体   English

图像和文本按钮与透明背景

[英]Image and Text button with transparent background

I'm trying to get a button to look like the one at the link I've posted below. 我正试图让一个按钮看起来像我在下面发布的链接。

It's a transparent button with image at the top and text on the bottom. 它是一个透明按钮,顶部是图像,底部是文本。 When a click occurs, the whole bounding box gets highlighted. 单击发生时,整个边界框将突出显示。

This is how the button looks like when pressed: http://i44.tinypic.com/24nle9e.png 这是按下时按钮的样子: http//i44.tinypic.com/24nle9e.png

Not sure if this effect is achieved with a Button or an ImageButton. 不确定使用Button或ImageButton是否实现了这种效果。 Any ideas? 有任何想法吗? Thanks. 谢谢。

dor506's answer is basically correct, but I'd use a ImageButton instead of a Button, and set it's background to transparent ( android:background="#00000000" - the 0 alpha is the key bit here) before applying your Drawable via android:src="@drawable/your_drawable_id" . dor506的答案基本上是正确的,但我使用ImageButton而不是Button,并在通过android:src="@drawable/your_drawable_id"应用你的Drawable之前将它的背景设置为透明( android:background="#00000000" - 这里的0 alpha是关键位) android:src="@drawable/your_drawable_id"

In case this helps, here is an example of a suitable Drawable: 如果这有帮助,这里是一个合适的Drawable的例子:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_hi" /> <!-- pressed -->
    <item android:drawable="@drawable/button_lo" /> <!-- default -->
</selector>

where 'button_hi' / 'button_lo' are the two images you are switching as a result of a click. 其中'button_hi'/'button_lo'是您因点击而切换的两个图像。

I don't think you can achieve this result with button/imagebutton properties. 我不认为你可以用button / imagebutton属性来实现这个结果。

here's a simple solution: you can create two images, one for the button when it isn't clicked, and one for the button when it is clicked (with the orange color behind..) 这是一个简单的解决方案:您可以创建两个图像,一个用于未单击时按钮,另一个用于单击按钮(后面带有橙色...)

then you should create a selector. 那么你应该创建一个选择器。 it is an xml which handle click/non click/focus image behaviour. 它是一个处理点击/非点击/聚焦图像行为的xml。

finally give the button the background of the selector 最后给按钮选择器的背景

        <Button android:layout_width="wrap_content" android:background="@drawable/selector" 
        android:layout_height="wrap_content" />

By the looks of it, it appears the image you're showing is an ImageButton with the image being a PNG image with transparent sections throughout it. 从它的外观来看,你看到的图像是一个ImageButton ,图像是一个PNG图像 ,整个区域都有透明的部分。 If you want to keep the button selected you should be able to mess with the button parameters: 如果你想保持选中按钮,你应该可以搞乱button参数:

android:state_selected = "true"

Try that. 试试吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM