我正在使用ImageButton
。但点击时我不明白。我GOOGLE了,许多人建议使用选择器在其他图像显示。有没有办法解决。通过只使用一个图像并突出显示或给它发光效果。以便用户知道该按钮已被点击。Android单击时突出显示一个图像按钮
回答
这其实并不是很难做到。你甚至不需要创建2个独立的.png文件或类似的东西。举例来说,如果你想拥有它有一个梯度的按钮,然后更改它,当按下按钮:
第1步: 创建默认按钮梯度(绘制/ default_button.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
<stroke android:width="1dp" android:color="#33364252" />
</shape>
步骤2:创建默认按钮被按下梯度(可拉伸/ default_button_pressed.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
<stroke android:width="1dp" android:color="#33364252" />
</shape>
步骤3:创建选择器(绘制/ default_button_selector.xml):
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" />
<item android:drawable="@drawable/default_button" />
</selector>
步骤4 (可选):为按钮创建风格(值/ style.xml):
<resources>
<style name="DefaultButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/default_button_selector</item>
</style>
</resources>
步骤5:使用按钮(布局/ main.xml中):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<button style="@style/DefaultButton" />
</RelativeLayout>
正如你所看到的,这并不是特别困难。
lol @Zack'正如你所见,做起来并不难。' – 2011-03-16 15:48:33
这很好,因为你可以使用相同的风格轻松添加一些按钮。 – 2011-03-16 16:17:09
如果我们需要具有不同背景图像的按钮,该怎么办? – Kishore 2012-08-21 09:25:51
为了不必为每个按钮设置多个drawable,我在触摸监听器中设置了图像按钮的颜色过滤器属性。
见代码在这里在另一篇文章:
+1这个固体解决方案。 – 2013-05-04 05:55:59
,而无需创建多个图像(压,正常等),甚至不必创建选择。使用setOnTouchListener而不是setOnClickListener。下面的代码会给你点击项目上的灰色覆盖。
((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageButton view = (ImageButton) v;
view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
// Your action here on button click
case MotionEvent.ACTION_CANCEL: {
ImageButton view = (ImageButton) v;
view.getBackground().clearColorFilter();
view.invalidate();
break;
}
}
return true;
}
});
这对我有用。请务必清除ACTION_UP上的颜色过滤器。 – 2014-06-06 15:36:27
我的ADT不知道'PorterDuff'是什么,所以后缀'android.graphics'(android.graphics.PorterDuff.Mode.SRC_ATOP)暗示代码完成 – 1owk3y 2014-12-01 22:48:02
帮了我很多,谢谢! – 2015-06-12 08:47:37
我设法做到了这一点! 首先在可绘制文件夹中声明buttonStyle.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/button_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/button_pressed" />
<item android:drawable="@drawable/button_normal" />
</selector>
然后创建button_pressed.xml在绘制文件夹中。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/semiTransparentGnfrBlueColor" />
<stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>
之后,你创建button_normal.xml在绘制文件夹中。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="3"
android:shape="rectangle">
<solid android:color="@color/gnfrBlueColor"/>
<stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>
您可以使用默认的颜色,但如果你想做到这一点跟我一样,你需要有一个名为颜色的文件。XML价值观文件夹,这个数值内:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="gnfrBlueColor" type="color">#2A3748</item>
<item name="semiTransparentGnfrBlueColor" type="color">#602A3748</item>
<integer-array name="androidcolors">
<item>@color/gnfrBlueColor</item>
<item>@color/semiTransparentGnfrBlueColor</item>
</integer-array>
</resources>
最后将它设置为您的按钮或什么:
savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle);
注意到我设置笔画颜色透明因为当你设置backgroundresource您的按钮变得更大,并与此伎俩它仍然是原创。
这是我可以通过程序实现的唯一方法。
如果您想通过XML做到这一点:
<Button
android:text="ENTRAR"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/buttonstyle"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/button1" />
- 1. Android:图像按钮或按钮突出显示效果时按
- 2. 单击按钮时突出显示仅图像
- 3. 单击下一个按钮时如何逐个显示图像?
- 4. 单击时突出显示图像,然后选中单选按钮
- 5. 单击单选按钮时如何突出显示标签?
- 6. 如何在按下按钮时突出显示一个按钮
- 7. 在按钮上单击显示图像
- 8. 如何在按下时突出显示图像按钮
- 9. 点击按钮时显示图像?
- 10. Android按钮==>设置突出显示的图像?
- 11. 单击按钮时出现图像
- 12. 单击按钮时无法显示下一个活动[Android]
- 13. 点击下一个按钮时显示图像
- 14. Android AndEngine按钮仍然突出显示
- 15. 如何在单击下一个按钮时逐个显示图像?
- 16. 突出显示与CSS单选按钮
- 17. 在android中按钮单击后显示图像
- 18. 如何制作一个带有颤动图像的按钮,当somone按下时突出显示图像?
- 19. 在单击按钮时在JLabel中显示图像时出现问题 - Java
- 20. 单击按钮时显示随机图像
- 21. 显示菜单,同时点击红宝石图像按钮?
- 22. 单击按钮时获取并显示图像
- 23. Xcode - 按钮突出显示不一致
- 24. 单击按钮时显示列表视图:Android
- 25. 防止单击时突出显示IconMenuIcon
- 26. 单击时突出显示文本
- 27. 表格视图单元格内的按钮在单元格上点击时显示突出显示的状态
- 28. Android 2.3.1相机takePicture()单击一个按钮的多个图像
- 29. jQuery手机按钮 - 点击时突出显示
- 30. 单击突出显示一组Divs
你可以告诉你使用的是创建图像按钮的代码? – Cristian 2011-03-16 15:27:54