How to change pressed color to other color of selector dynamically?(如何将按下的颜色动态更改为选择器的其他颜色?)
本文介绍了如何将按下的颜色动态更改为选择器的其他颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的列表选择器:(Item_seltor.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/orange" />
<item android:state_focused="true" android:drawable="@color/orange" />
<item android:state_pressed="true" android:drawable="@color/orange" />
<item android:drawable="@color/transparent" />
</selector>
这是我的List行(row_item.xml)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/alarm_item_drawer_selector"
>
<ImageView
android:id="@+id/item_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:paddingTop="5dp"
android:paddingBottom="2dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/item_title"
style="@style/DrawerItemTextStyle"
android:textColor="@color/text_drawer_item_selector"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_icon" />
</RelativeLayout>
我想要动态地将android:STATE_PRESSESS="true"android:Drawable="@COLOR/ONGLE"更改为其他颜色。我的目标是让每一行的压榨颜色不同。可以这样做吗?
推荐答案
影响每个项目一个新的StateListDrawable。
StateListDrawable stateListDrawable= new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
stateListDrawable.addState(new int[] {android.R.attr.state_focused}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
stateListDrawable.addState(new int[] {android.R.attr.state_selected}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
stateListDrawable.addState(new int[] {}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
view.setBackgroundDrawable(stateListDrawable);
这篇关于如何将按下的颜色动态更改为选择器的其他颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何将按下的颜色动态更改为选择器的其他颜色?


基础教程推荐
猜你喜欢
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01