在iOS中,如果我将按钮的背景设置为图像,那么当我按下按钮时,按钮的整个内容(包括文本)将被遮挡。我可以在Android中实现相同的效果,还是必须针对不同的状态使用不同的图像?另外,即使我对不同的状态使用不同的图像,如何使文本也带有阴影?一种肮脏的方法是为按钮设置一个 OnClickListener 并在按下时以编程方式隐藏文本,但是还有其他方法吗?
In iOS, if I set a button's background to an image, when I press the button, the whole content of the button (including the text) will be shadowed. Can I achieve the same effect in Android, or do I have to use different images for different states? Also, even if I use different images for different states, how do I make the text also shadowed? A dirty way is to set a OnClickListener
to the button and programatically shadow the text when pressed, but are there any other ways?
发布评论
评论(3)
一段时间以来,我一直在尝试寻找解决方案,但找不到任何东西,所以我想出了一个非常简洁的解决方案,适用于所有图像按钮。就像 iOS 一样。
创建一个黑色、10% 透明的图像并将其另存为 PNG。我将其命名为“button_pressed.png”。您可以使用此图像, http://img84.imageshack.us/img84/7924/ buttonpressed.png
创建一个名为相关内容的可绘制对象,我将其称为“button_pressed_layout.xml”
现在将按钮图像放入 LinearLayout 中,然后将 Button 放入 LinearLayout 中。在按钮中使用button_pressed_layout.xml作为背景。
<前><代码>
<按钮
android:id="@+id/myButtonId"
android:text="@string/继续"
安卓:layout_width =“fill_parent”
安卓:layout_height =“fill_parent”
android:background="@drawable/button_pressed_layout"
机器人:文本颜色=“#FFF”
android:textSize="16dp" >
就是这样!
I´ve been trying to find a solution for this for a while now but couldn´t find anything so I came up with a pretty neat solution which works on all image buttons. Just like iOS.
Create a Black, 10% transparent image and save it as PNG. I call it button_pressed.png. You can use this image, http://img84.imageshack.us/img84/7924/buttonpressed.png
Create a drawable called something relevant, I call it "button_pressed_layout.xml"
Now put your button image in the LinearLayout and then the Button inside the LinearLayout. In the Button use button_pressed_layout.xml as background.
That´s it!
为了在不创建第二个按钮图像的情况下获得按下状态,我创建了一个 OnTouchListener 来更改按钮的 Alpha。这不是最漂亮的按下状态,但给出了有效的自动行为。
在您的 Activity 中,将此侦听器应用于每个按钮:
To get a pressed-state without creating a 2nd button image, I created an OnTouchListener to change the button's alpha. This is not the most beautiful pressed state, but gives an effective automatic behavior.
In your Activity, apply this listener to each button:
为了给按钮本身不同的效果,我相信您需要使用自定义图像,如此处,以及不太详细的 这里 至于文字,这篇文章< /a> 看起来它可以解决阴影问题
In order to give the button itself different effects, I believe you need to use custom images, as seen here and, in less detail here As for the text, this post looks like it would do the trick for the shadow