【问题标题】:What is the default drawable for pressing a list item按下列表项的默认可绘制对象是什么
【发布时间】:2011-03-31 05:39:45
【问题描述】:

当用户按下 ListView 项 (android:state_pressed="true") 时,它会闪烁黄色阴影(或者您可以按住)。

这是什么可绘制的?我创建了自己的选择器,因为我想要自己的 ListView 项目颜色,但我失去了按下的颜色。

有一个关于皮肤按钮的 Android 文档引用 #ffff0000,但这会产生红色。

有谁知道它是什么以及如何引用它?

【问题讨论】:

    标签: android state selector


    【解决方案1】:

    颜色定义为#AARRGGBB,其中 AA 表示 alpha(透明度)值,RR 表示红色量,GG 表示绿色量,BB 表示蓝色量。因此#ffff0000 是实心的并且全是红色的。如果你想要橙色,你想添加一些绿色,即:#ffffA500。谷歌 RGB 颜色值以查看颜色页面及其 rgb 值。

    【讨论】:

    • 是的,我有这个。我只是想知道Android的默认设置。不过感谢您的回复
    【解决方案2】:

    您所说的是 Android 操作系统的内置选择器。

    在您的可绘制文件夹中使用 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="#YOURCOLOR" /> 
      <item android:color="#FE896F" /> 
    </selector>
    

    然后在你的 XML 文件中有你的 ListView。

    android:textColor="@drawable/highlight" //For text to appear like YOURCOLOR
    //or if you wish the background
    android:background="@drawable/highlight" //For the background to appear like YOURCOLOR
    

    我希望就是这样,告诉我这是否有效!

    【讨论】:

    • 感谢您的回复。我已经创建了一个这样的选择器文件。我使用 Romain Guy 的技巧在我设置了背景颜色的列表项上维护滚动板选择器。我正在寻找的是 Android 的默认 state_pressed 颜色。
    • 你可以做一个丑陋的黑客攻击,比如打印按下的项目,然后查找 RGB 颜色,但当然,默认颜色是在某处声明的。问题是如何找到它
    • 它实际上看起来更像是某个值的渐变
    • 我相信它是list_selector_background_pressed,但我不知道如何引用它。我试过@android:drawable/list_selector_background_pressed,但无济于事
    • 这就像一个谜;如果我知道解决方案,我将不胜感激。否则;不能让程序员改变这个特定的东西吗?
    【解决方案3】:

    我在照片编辑应用程序中使用了颜色选择器工具来查找渐变的外部和内部颜色并自己制作。

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="#ffb300"
            android:centerColor="#ffc800"
            android:endColor="#ffb300"
            android:angle="270"/>
    </shape>
    

    【讨论】:

      【解决方案4】:

      默认系统资源可以在&lt;android-sdk&gt;/platforms/android-&lt;version&gt;/data/res中找到。特别是,列表选择器在drawable/list_selector_background.xml中定义:

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
          <item android:state_window_focused="false"
              android:drawable="@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="@drawable/list_selector_background_disabled" />
          <item android:state_focused="true" android:state_enabled="false"
              android:drawable="@drawable/list_selector_background_disabled" />
      
          <item android:state_focused="true" android:state_pressed="true"
              android:drawable="@drawable/list_selector_background_transition" />
          <item android:state_focused="false" android:state_pressed="true"
              android:drawable="@drawable/list_selector_background_transition" />
      
          <item android:state_focused="true"
              android:drawable="@drawable/list_selector_background_focus" />
      
      </selector>
      

      在印刷机上显示的可绘制对象list_selector_background_transition 不是单一颜色,而是两个 9 色块图像,一个黄色和一个白色,它们之间有动画过渡。

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

      【讨论】:

      • 简而言之:你要引用@android:drawable/list_selector_background_transition
      • 当我构建自己的选择器时,为什么我不能引用@android:drawable/list_selector_background_transition?
      • 我也注意到了这一点。简而言之,我不知道为什么。但是,您可以在项目中创建自己的 list_selector_background_transition 副本。然后你可以参考那个。
      • @Jonas 这是因为它们不公开可见。
      • 我认为对于新的 Android 版本,这个选择器 (list_selector_background) 没有被使用,因为它们仍然是黄色的,而新的选择器是蓝色的(或灰色,从 Kitkat 开始)。问题是,你如何获得新的?
      猜你喜欢
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 2011-03-13
      • 2019-12-06
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多