Android- 改变颜色并同时在布局上产生波纹
Android- Change colour and have ripples on layout at the same time
我有一个LinearLayout
。我对它应用了涟漪效应,效果很好。我正在做的是当用户点击它时,它应该将其颜色更改为白色(默认为灰色)。如果再次单击,它应该再次变为灰色。这是我使用 layout.setBackgroundColor(Color.WHITE);
实现的。
但我想要涟漪和这个颜色切换。如果我使用 setBackgroundColor
,涟漪效应就会消失。关于如何执行此操作的任何线索?
XML 涟漪(LinearLayout
的背景):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/divider">
<item android:drawable="@color/background" />
</ripple>
请为 pre-lollipop 设备提出等效的解决方案。这个LinearLayout
在pre-lollipop的背景是,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/divider" android:state_pressed="true" />
<item android:drawable="@android:color/transparent" />
</selector>
不要使用 setBackgroundColor,而是使用 setImageDrawable()
并传递 xml 布局的 ID (R.drawable.your_layout)。
定义两种不同的 xml 布局:一种背面为灰色,另一种为白色背景。
白底
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/divider">
<item android:drawable="#ffffff" />
</ripple>
灰色背景
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/divider">
<item android:drawable="888888" />
</ripple>
我有一个LinearLayout
。我对它应用了涟漪效应,效果很好。我正在做的是当用户点击它时,它应该将其颜色更改为白色(默认为灰色)。如果再次单击,它应该再次变为灰色。这是我使用 layout.setBackgroundColor(Color.WHITE);
实现的。
但我想要涟漪和这个颜色切换。如果我使用 setBackgroundColor
,涟漪效应就会消失。关于如何执行此操作的任何线索?
XML 涟漪(LinearLayout
的背景):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/divider">
<item android:drawable="@color/background" />
</ripple>
请为 pre-lollipop 设备提出等效的解决方案。这个LinearLayout
在pre-lollipop的背景是,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/divider" android:state_pressed="true" />
<item android:drawable="@android:color/transparent" />
</selector>
不要使用 setBackgroundColor,而是使用 setImageDrawable()
并传递 xml 布局的 ID (R.drawable.your_layout)。
定义两种不同的 xml 布局:一种背面为灰色,另一种为白色背景。
白底
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/divider">
<item android:drawable="#ffffff" />
</ripple>
灰色背景
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/divider">
<item android:drawable="888888" />
</ripple>