我有一个带有透明图像的ImageButton。我改变了ImageButton的背景颜色。
当按下具有默认颜色的ImageButton时,该按钮显示它已被“发光”按下很短的时间。但是当背景颜色改变时,“发光”效果不会显示。不同的行为与图像无关,因为当按钮具有标准的“灰色”颜色时会显示“发光”效果。
如何在彩色图像按钮中产生“发光”效果?
P.S。我知道如何在单击事件上更改ImageButton的颜色。但我希望与默认的灰色ImageButton具有相同的效果。 Button对象也会发生同样的事情(发光不显示)。
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用ColorStateList来定义按钮的pressed
颜色。
您应该查看我上面链接的文档以获取详细信息。以下是文档中的一个简单示例,其中介绍了基础知识:
保存在res / color / button_text.xml的XML文件:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#ffff0000"/> <!-- pressed --> <item android:state_focused="true" android:color="#ff0000ff"/> <!-- focused --> <item android:color="#ff000000"/> <!-- default --> </selector>
此布局XML将颜色列表应用于视图:
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_text" android:textColor="@color/button_text" />
在您的情况下,您需要将android:background
设置为指向新的XML资源,而不是android:textColor
。
希望有所帮助。
答案 2 :(得分:0)
android具有发光效果作为透明背景资源,所以我通过在按钮后面放置彩色视图并将按钮背景设置为透明发光效果来解决问题。
例如在我的xml中:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/custom_color"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
和我的onCreate:
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
Button mButton = (Button) findViewById(R.id.button);
mButton.setBackgroundResource(outValue.resourceId);
仅适用于您LinearLayout
中只有一个按钮,如果您想在其中添加其他内容,可以使用RelativeLayout
内的视图,其参数与按钮相同。
如果您有任何其他问题,请告诉我!