修正:"安卓检测对焦/ pressed颜色" [英] Fixed: "Android: Detecting focus/pressed color"

查看:112
本文介绍了修正:"安卓检测对焦/ pressed颜色"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来检测对焦/ pressed颜色按钮等元素。 这是必要的,因为我正在开发新的组件,它是重要的,那些看起来平台的一部分。 这些颜色是橙色的Andr​​oid SDK和绿色的HTC SenseUI。 如果我能发现那个颜色我的组件将显示为平台的两个版本的一部分。

I'm trying to detect the focus/pressed color for button and other elements. This is needed because I'm developing new components and it's important that those look as part of platform. Those colors are ORANGE on android sdk and GREEN on HTC SenseUI. If I could detect that color my component will look as part of platform on both version.

有谁知道如何做到这一点?

Does anyone knows how to do this?

它可以创建选择,它使用自定义图像默认状态和平台默认的焦点/选择。

It's possible to create "selector" which uses custom image for default state and platform default for focus/selection.

为此请按照下列步骤:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:drawable="@android:drawable/btn_default" >
    </item>

    <item android:state_focused="true"
        android:drawable="@android:drawable/btn_default" >
    </item>

    <item 
        android:drawable="@drawable/btn_default_red" >
    </item>
</selector>

2)从文件夹... / Android的SDK-MAC /平台/ Android的1.5 /数据/ RES /绘制/拍照btn_default_ pressed.9.png并改变颜色,只要你喜欢(我需要将其更改为红色,并为此GIMP是足够了)。

2) from folder ".../android-sdk-mac/platforms/android-1.5/data/res/drawable/" take picture "btn_default_pressed.9.png" and change color as you like (I needed to change it to red and for this GIMP is enough).

3)放在资源/绘制(例如,名为btn_default_red.9.png)

3) place altered picture in "res/drawable" (e.g. with name "btn_default_red.9.png")

4)定义按钮:

<Button
    android:id="@+id/info_button"
    android:layout_width="wrap_content" 
    android:layout_height="37dip"
    android:layout_marginTop="1dip"
    android:background="@drawable/red_button"
    android:text="[Info]" />

这就是全部。

That's all.

这是结果:

This is result:

推荐答案

我有这个问题了。正如已经说过的,问题是,背景不是简单的颜色,它们可绘制,可以采取对所有种出场。然而,我发现了一个变通办法,可能会有所帮助。如果您的自定义组件看起来像一个已经存在的,如一个按钮或ListView项,你可以窃取他们的背景/选择器,并设置为背景的自定义组件。例如,在你的自定义组件的构造函数:

I had this problem too. As already stated, the problem is that the backgrounds aren't simple colors, they're Drawables that could take on all kinds of appearances. However, I found a work-around that may help. If your custom component looks something like an existing one, e.g. a Button or ListView entry, you can just steal their background/selector and set that as the background for your custom component. E.g., in your custom component constructor:

  setBackgroundDrawable(new Button(context).getBackground());

或一个背景更适合列表类似的元件:

or for a background more suitable for list-like components:

  setBackgroundDrawable(new ListView(context).getSelector());

您可能要稍微优化了code,但你的想法。

You may want to optimise that code somewhat, but you get the idea.

这篇关于修正:&QUOT;安卓检测对焦/ pressed颜色&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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