【问题标题】:Android selector long press with colorsAndroid选择器长按颜色
【发布时间】:2015-01-19 10:54:22
【问题描述】:

这是我需要的:

显示您按下它的选择器。但是这个选择器也处理长按颜色转换。我看了很多,发现this。这基本上是我需要的,但这仅适用于图像,而不适用于颜色。这是我现在的代码:

default_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:drawable="@android:color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@android:color/transparent" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@android:color/transparent" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/default_selector_transition" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/default_selector_transition" />
    <item android:state_focused="true"                                                             android:drawable="@color/selection_grey" />
    <item android:state_activated="true" android:drawable="@color/selection_grey"/>
    <item android:drawable="@android:color/transparent" />
</selector>

default_selector_transition.xml:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_pressed_default"  />
    <item android:drawable="@drawable/list_longpressed_default"  />
</transition>

default_selector_transition.xml 中的可绘制对象现在有 9 个补丁。我想知道是否可以将其更改为颜色。

我尝试了什么:

  • 只需在其中放置颜色而不是可绘制对象
  • 在其中添加可绘制的颜色 (&lt;drawable name="selection_grey_drawable"&gt;#BDBDBD&lt;/drawable&gt;)

希望大家给我一个解决方案!

【问题讨论】:

    标签: android colors long-press android-selector android-transitions


    【解决方案1】:

    使用颜色而不是可绘制对象对您不起作用?

    如果没有,您仍然可以尝试创建一个单独的 drawable 文件,其中包含 shapes。比如这样:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/your_color_1" />
    </shape>
    

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/your_color_2" />
    </shape>
    

    然后将这些文件链接到您的default_selector_transition.xml:

    <transition xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/first_shape"  />
        <item android:drawable="@drawable/second_shape"  />
    </transition>
    

    编辑:

    主要问题是你的代码根本不适合我,即使有 9 个补丁......

    Android 开发者documentation 说:

    &lt;item&gt; 定义一个可绘制对象以用作可绘制过渡的一部分。 必须是 &lt;transition&gt; 元素的子元素。接受孩子&lt;bitmap&gt; 元素。

    因此,如果您的代码适合您,您仍然可以尝试欺骗drawable 使用正在创建的带有颜色的bitmap 子级? 像这样的东西应该可以工作:

    <bitmap
        android:src="@drawable/dummy_bitmap"
        android:tint="@color/your_color"
        android:tileMode="repeat"/>
    

    其中dummy_bitmap 只是一些可“填充”此位图中所需的src 字段的可绘制对象。

    可能是这样的:

    <transition xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <bitmap
                android:src="@drawable/NOT_transparent_dummy_bitmap"
                android:tint="@color/your_color_1"
                android:tileMode="repeat"/>
        </item>
        <item>
            <bitmap
                android:src="@drawable/NOT_transparent_dummy_bitmap"
                android:tint="@color/your_color_2"
                android:tileMode="repeat"/>
        </item>
    </transition>
    

    【讨论】:

    • 我刚试过这个,不幸的是这不起作用。这可能是一个不错的其他选择。
    • 好吧,我只是按照你说的做了,在drawable文件夹中创建了两个文件。两种形状。一个是灰色 (#BDBDBD),另一个是深灰色 (#9E9E9E)。我把你说的那些形状放在过渡文件中,但是长按时它只保持一种颜色
    • 很好的尝试,我真的认为这个会起作用。但不幸的是它没有。我使用了透明图像,色调再次变灰。这次完全没有选择
    • 该死,我的想法用完了:) 祝你好运:p 如果你找到解决方案,请将其粘贴到此线程的某个位置。
    • 等等...你用的是透明图片? tint 仅在其不透明时才有效。它可以是一个 1x1 像素的 png 文件,因为存在 tileMode="repeat"(任何颜色,但不透明)
    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2011-01-27
    • 2012-12-14
    相关资源
    最近更新 更多