【问题标题】:Change color on tapping ListView item with Gradient Drawable使用 Gradient Drawable 在点击 ListView 项目时更改颜色
【发布时间】:2013-01-17 07:14:41
【问题描述】:

我可以使用显示正确输出的自定义适配器设置列表视图。每个列表项也有一个渐变样式,请参阅下面的 drawable.xml 如何在点击其中任何一个时将项目背景更改为其他渐变。我可以为每个项目设置不同的渐变吗?

<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<gradient 
android:startColor="#FFFAFAFA" 
android:centerColor="#FFFFFFFF" 
android:endColor="#FFFAFAFA" 
android:angle="90"/>
</shape>

我的文件...

activity_main.xml....主 xml 文件

MainActivity.java....主要活动

Item.java.... 项目选择器方法

ItemAdaptor.... 自定义适配器

list_item.xml..... 项目(textView)样式

【问题讨论】:

    标签: java android listview drawable


    【解决方案1】:

    点击时我得到了颜色变化

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:background="@drawable/listitem_states" >
    

    使用 res/values/listitem_states.xml:

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
       android:drawable="@color/listitem_active_color" ></item>
    <item
        android:state_pressed="true"
       android:drawable="@color/listitem_active_color" ></item>
    <item 
        android:drawable="@color/listitem_bg_color" /> 
    </selector> 
    

    我想你可以很容易地适应渐变。

    【讨论】:

      【解决方案2】:

      像这样试试..

      在drawable中添加这个

      <?xml version="1.0" encoding="utf-8"?>
      
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <!-- Selector style for listrow -->
      
          <item
           android:state_selected="false"
              android:state_pressed="false"
              android:drawable="@drawable/gradient_bg" />
      
          <item android:state_pressed="true"
              android:drawable="@drawable/gradient_bg_hover" />
      
          <item android:state_selected="true"
           android:state_pressed="false"
              android:drawable="@drawable/gradient_bg_hover" />
      </selector>
      

      添加布局

      <ListView
              android:id="@+id/list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:divider="#FFFFFF"
              android:dividerHeight="1dp"
              android:listSelector="@drawable/list_selector" />
      

      【讨论】:

        猜你喜欢
        • 2011-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 2012-03-10
        • 1970-01-01
        • 2020-08-29
        相关资源
        最近更新 更多