在Android的按键采用效果使用图像 [英] Using Image in a Android Button with effects

查看:139
本文介绍了在Android的按键采用效果使用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(现在我所遇到的StackOverflow上相关的问题,但遗憾的是没有解决方案的工作对我来说这就是我为什么要问这个单独)

(Now I have come across related questions on StackOverflow but unfortunately none of the solutions worked for me which is why I had to ask this separately)

我是新手到Android。问题:我需要有一个充当按钮的图像。现在我明白了,这可以通过一个标准的按钮使用图像或使用的东西称为ImageButton的(我的猜测是强烈建议,虽然我不知道为什么)来实现。

I am a Novice to Android. The problem: I need to have an image that acts as a button. Now I understand that this can be achieved by either using an image on a standard button or by using something called as "ImageButton" (which I guess is highly recommended although I have no idea why).

要求:我需要去对这个问题的详细指导。具体来说,我认为把图像上标准的键是比较容易,直到我面临两个主要问题:我无法设置图像的中心(感谢绘制,顶部,底部,left6,右),一旦我改变背景颜色相匹配的活动屏幕的背色,按钮的效果消失。简单地说,我需要有一个图像充当按钮或其中有三个效果的图像一个按钮,适度宽松的方式:集中,pressed和默认。我用的ImageButton但当时我不知道如何使自定义形状或9patch图像给它的所有预期的效果,我是pretty满意的Andr​​oid提供的默认按钮。所有我只是需要的东西就像一个背景悬停在图片或诸如此类的东西,表示用户的图像被pressed并已生成的事件!

Requirements: I need detailed guidance for going about this problem. Specifically, I thought the "putting-image-on-standard-button" was easier until I faced two major issues: I was unable to set the image in the center (thanks drawable-top,bottom,left6,right) and once I changed the background color to match that of the Activity screen's back-color, the button effect disappeared. Simply put, I need a moderately easy way of having an image act as a button or a button with an image which has all three effects: focussed, pressed and default. I used ImageButton but then I did not know how to make custom shapes or 9patch images to give it all the desired effects, I am pretty satisfied with the default button that android provided. All I simply need is something like a background hover over the image or something of that sort which indicates the user that the image is being pressed and the event has been generated!

有人可以帮我这吗?我需要的UI看起来体面,因此,需要在我的图像/按钮,相应的影响。在此先感谢:)

Can someone please help me out with this? I need the UI to look decent and therefore, need the corresponding effects on my image/button. Thanks in Advance :)

这是我的图标图像:

This is my icon-image:

我希望有某种解决此悬停效果,表明图像已被pressed就像任何普通按钮。

I wish to have some sort of hover effect around this that indicates that the image has been pressed just like any normal button.

推荐答案

使用的ImageButton 和<一个href=\"https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList\">StateList可绘制。你需要选择器,用于不同的按钮状态。您可以指定不同的绘制不同的状态,以模仿<大骨节病>的onfocus 或<大骨节病>上pressed 正常按钮的作用。

Use ImageButton and StateList Drawable. You need selector for different button's states. You can assign different drawable for different state to imitate the onFocus or onPressed effect on normal button.

这是 selector.xml 绘制文件夹下的 RES

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
      android:drawable="@color/cyan"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@color/cyan"/> <!-- focused state -->
<item android:drawable="@android:color/transparent"/> <!-- default state -->

</selector>

这是 color.xml res文件夹

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <color name="cyan">#33B5E5</color>
</resources>

的ImageButton 的<大骨节病>的src 你的形象,并设置<大骨节病>背景 selector.xml

Set the ImageButton's src to your image and set the background to selector.xml.

这是最终的结果是:

后

有一个很好的教程在这里:的Andr​​oid的ImageButton selector例子

There is a good tutorial here: Android ImageButton Selector Example

这篇关于在Android的按键采用效果使用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆