使用 ?attr/selectableItemBackground 作为背景时如何修改波纹颜色?
How can I modify ripple color when using ?attr/selectableItemBackground as background?
我看到了一些SO问题,他们给出了一些可能的方法来实现我想要的。例如:
在styles.xml中使用colorControlHighlight
属性。
这是我的风格-v21.xml:
<style name="SelectableItemBackground">
<item name="android:colorControlHighlight">#5677FC</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
还有我的小部件:
<TextView
android:id="@+id/tv_take_photo_as_bt"
android:layout_width="280dp"
android:layout_height="48dp"
android:text="@string/act_take_photo"
style="@style/SelectableItemBackground"/>
而且它不起作用。我也试过把parent="Theme.AppCompat
加成"SelectableItemBackground"样式,或者改成colorControlHighlight(no android: prefix)"
,或者改成?android:attr/selectableItemBackground
,都没有用。
在布局中使用 backgroundTint
属性。
所以我将 android:backgroundTint="#5677FC"
添加到我的 TextView
。还是没用。然后我尝试将 android:backgroundTintMode
更改为 src_in
和 src_atop
,但它们没有任何区别。
那么,当我使用 ?attr/selectableItemBackground
作为背景时,如何改变波纹颜色。我只关注 Lollipop 及以上。提前致谢!
终于找到了解决方法:与其直接在主题SelectableItemBackground
中使用android:colorControlHighlight
,不如写另一种风格:
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/ripple_color</item>
</style>
然后:
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
最后在layout.xml中添加style="@style/SelectableItemBackground"
到View
。
更新于 2016/8/26
N发布后,我发现有时我们无法使用此方法为某些View
(例如CardView
)设置波纹颜色。现在我强烈建议开发者使用 RippleDrawable
,也可以在 xml 中声明。这是一个例子:
我想在API21以上的用户touches/clicks一个CardView
时显示一个连锁反应,当然Lollipop之前应该有另一种反馈。所以我应该写:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/selectable_item_background"/>
和 selectable_item_background
在 drawable
文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:drawable="@color/color_clicked" />
</selector>
selectable_item_background
在 drawable-v21
文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ripple_black" />
</selector>
最后,drawable
(或drawable-v21
)文件夹中的ripple_black
:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/color_clicked"
tools:ignore="NewApi" /> <!--you can remove this line if it's in v21 folder-->
就是这样。对于其他视图,也许您应该使用 android:background="@drawable/selectable_item_background"
。不要忘记为它们设置 OnClickListener
、OnTouchListener
或类似的东西,否则波纹不会显示。
接受的答案是错误的。
正确的使用方法是流亭在评论中提到的。
使用 colorControlHighlight
而不是 android:colorControlHighlight
将默认值 colorControlHighlight
从 AppCompat
更改为
* 请参考主题部分中的 http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html *
Lollipop+ 之前和 Lollipop+ 设备的涟漪效应
harrane 和 liuting 是对的。接受的答案不是最好的方法。
让我在代码中展示如何更改 pre-Lollipop 版本和更高版本
的波纹颜色
您的 AppTheme 应该继承任何 AppCompat 主题并包含 colorControlHighlight 属性(没有 'android:' 前缀)
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorControlHighlight">#40ffffff</item>
</style>
您的视图应包含 clickable="true"(或应以编程方式设置点击侦听器)并且背景应为“?attr/selectableItemBackgroundBorderless”或“?[=67=” ]" :
<LinearLayout
...
android:clickable="true"
android:background="?attr/selectableItemBackgroundBorderless"/>
注意:如果您的父视图有白色背景,您将看不到波纹效果,因为它是白色的。更改不同颜色的 colorControlHighlight 值
另外,如果你想在不同的活动中使用不同的波纹颜色,你可以在 Manifest 文件中为每个 activity 设置个人主题,例如:
<activity
android:name="com.myapp.GalleryActivity"
android:theme="@style/RedRippleTheme"
/>
同一个片段的不同波纹颜色 activity?
您可以在运行时为每个片段更改 Activity 主题的属性。只需在片段被您的自定义样式膨胀并应用于当前主题之前覆盖它们:
在values/styles.xml
<style name="colorControlHighlight_blue">
<item name="colorControlHighlight">@color/main_blue_alpha26</item>
</style>
然后,在 onCreateView()
中 inflation 之前的片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getContext().getTheme().applyStyle(R.style.colorControlHighlight_blue, true); //blue ripple color
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
此样式仅适用于此片段
不同视图的波纹颜色不同? (棒棒糖+)
您可以使用以下方法分别更改每个视图的波纹颜色
colorControlHighlight
属性,如果直接将它们应用到视图,它不起作用:
<TextView
...
colorControlHighlight="#40ffffff"/> <!-- DOESN'T WORK -->
您应该将其应用为主题:
<TextView
...
android:theme="@style/colorControlHighlight_blue"/>
P.S。此外,如果您遇到未知的纹波问题并且无法弄清楚,有时这种方法会有所帮助。
在我的例子中,我使用了第 3 方滑动库,它扰乱了整个布局的涟漪效应,并将这个主题明确添加到对我来说可行的所有可点击视图中。
使用前景属性作为selectableItemBackground
和背景属性作为你想要的颜色。
android:foreground="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/white"
它在 API +21 上显示带有颜色的波纹效果,在 API -21 上显示简单的灰色背景。
添加此样式:
<style name="AppTheme.MyRipple">
<item name="colorControlHighlight">@color/your_color</item>
<item name="android:background">?selectableItemBackgroundBorderless</item>
</style>
并将其设置为视图:
<Button
...
android:theme="@style/AppTheme.MyRipple" />
这段代码对我有用,可以产生涟漪:
public static void setRippleDrawable(View view, int normalColor, int touchColor) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
RippleDrawable rippleDrawable = new RippleDrawable(ColorStateList.valueOf(touchColor), view.getBackground(), null);
view.setBackground(rippleDrawable);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(touchColor));
stateListDrawable.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(touchColor));
stateListDrawable.addState(new int[]{}, new ColorDrawable(normalColor));
view.setBackground(stateListDrawable);
}
} catch (Exception e) {
Log.e(LOG_TAG, "" + e);
}
}
我没有找到修改 selectableItemBackground 属性的方法。
这就是为什么我像上面那样做。
在深黑色主题(或任何其他)应用程序中尝试使用如下
首先在 drawable
文件夹中创建 ripple_effect.xml
并添加代码
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#f5f5f5">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f5f5f5" />
</shape>
</item>
</ripple>
然后将背景设置为您的任意视图,例如 Linear layout, Button, TextView
等
<TextView
android:id="@+id/tvApply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect"
android:clickable="true"
android:text="APPLY"
android:textColor="#FFFFFF"
android:gravity="center"
android:textSize="@dimen/_8sdp"
android:padding="@dimen/_8sdp"
android:focusable="true" />
使用以下步骤:
1. Make changes to button view in your layout.xml
2. Add new styles in styles.xml
your_layout.xml
<Button
android:id="@+id/setup_submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_in"
android:textStyle="bold"
android:background="@color/colorPrimary"
android:textColor="@color/white"
style="@style/SelectableItemBackground"
android:foreground="?android:attr/selectableItemBackground"/>
-The style attribute calls the style that we created.
-Foreground attribute calls the andorid's default selectable attribute.
styles.xml
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/white</item>
</style>
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
我看到了一些SO问题,他们给出了一些可能的方法来实现我想要的。例如:
在styles.xml中使用
colorControlHighlight
属性。这是我的风格-v21.xml:
<style name="SelectableItemBackground"> <item name="android:colorControlHighlight">#5677FC</item> <item name="android:background">?attr/selectableItemBackground</item> </style>
还有我的小部件:
<TextView android:id="@+id/tv_take_photo_as_bt" android:layout_width="280dp" android:layout_height="48dp" android:text="@string/act_take_photo" style="@style/SelectableItemBackground"/>
而且它不起作用。我也试过把
parent="Theme.AppCompat
加成"SelectableItemBackground"样式,或者改成colorControlHighlight(no android: prefix)"
,或者改成?android:attr/selectableItemBackground
,都没有用。在布局中使用
backgroundTint
属性。所以我将
android:backgroundTint="#5677FC"
添加到我的TextView
。还是没用。然后我尝试将android:backgroundTintMode
更改为src_in
和src_atop
,但它们没有任何区别。
那么,当我使用 ?attr/selectableItemBackground
作为背景时,如何改变波纹颜色。我只关注 Lollipop 及以上。提前致谢!
终于找到了解决方法:与其直接在主题SelectableItemBackground
中使用android:colorControlHighlight
,不如写另一种风格:
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/ripple_color</item>
</style>
然后:
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
最后在layout.xml中添加style="@style/SelectableItemBackground"
到View
。
更新于 2016/8/26
N发布后,我发现有时我们无法使用此方法为某些View
(例如CardView
)设置波纹颜色。现在我强烈建议开发者使用 RippleDrawable
,也可以在 xml 中声明。这是一个例子:
我想在API21以上的用户touches/clicks一个CardView
时显示一个连锁反应,当然Lollipop之前应该有另一种反馈。所以我应该写:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/selectable_item_background"/>
和 selectable_item_background
在 drawable
文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:drawable="@color/color_clicked" />
</selector>
selectable_item_background
在 drawable-v21
文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ripple_black" />
</selector>
最后,drawable
(或drawable-v21
)文件夹中的ripple_black
:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/color_clicked"
tools:ignore="NewApi" /> <!--you can remove this line if it's in v21 folder-->
就是这样。对于其他视图,也许您应该使用 android:background="@drawable/selectable_item_background"
。不要忘记为它们设置 OnClickListener
、OnTouchListener
或类似的东西,否则波纹不会显示。
接受的答案是错误的。
正确的使用方法是流亭在评论中提到的。
使用 colorControlHighlight
而不是 android:colorControlHighlight
将默认值 colorControlHighlight
从 AppCompat
* 请参考主题部分中的 http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html *
Lollipop+ 之前和 Lollipop+ 设备的涟漪效应
harrane 和 liuting 是对的。接受的答案不是最好的方法。 让我在代码中展示如何更改 pre-Lollipop 版本和更高版本
的波纹颜色您的 AppTheme 应该继承任何 AppCompat 主题并包含 colorControlHighlight 属性(没有 'android:' 前缀)
<!-- Application theme. --> <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="colorControlHighlight">#40ffffff</item> </style>
您的视图应包含 clickable="true"(或应以编程方式设置点击侦听器)并且背景应为“?attr/selectableItemBackgroundBorderless”或“?[=67=” ]" :
<LinearLayout ... android:clickable="true" android:background="?attr/selectableItemBackgroundBorderless"/>
注意:如果您的父视图有白色背景,您将看不到波纹效果,因为它是白色的。更改不同颜色的 colorControlHighlight 值
另外,如果你想在不同的活动中使用不同的波纹颜色,你可以在 Manifest 文件中为每个 activity 设置个人主题,例如:
<activity
android:name="com.myapp.GalleryActivity"
android:theme="@style/RedRippleTheme"
/>
同一个片段的不同波纹颜色 activity?
您可以在运行时为每个片段更改 Activity 主题的属性。只需在片段被您的自定义样式膨胀并应用于当前主题之前覆盖它们:
在values/styles.xml
<style name="colorControlHighlight_blue">
<item name="colorControlHighlight">@color/main_blue_alpha26</item>
</style>
然后,在 onCreateView()
中 inflation 之前的片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getContext().getTheme().applyStyle(R.style.colorControlHighlight_blue, true); //blue ripple color
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
此样式仅适用于此片段
不同视图的波纹颜色不同? (棒棒糖+)
您可以使用以下方法分别更改每个视图的波纹颜色
colorControlHighlight
属性,如果直接将它们应用到视图,它不起作用:
<TextView
...
colorControlHighlight="#40ffffff"/> <!-- DOESN'T WORK -->
您应该将其应用为主题:
<TextView
...
android:theme="@style/colorControlHighlight_blue"/>
P.S。此外,如果您遇到未知的纹波问题并且无法弄清楚,有时这种方法会有所帮助。 在我的例子中,我使用了第 3 方滑动库,它扰乱了整个布局的涟漪效应,并将这个主题明确添加到对我来说可行的所有可点击视图中。
使用前景属性作为selectableItemBackground 和背景属性作为你想要的颜色。
android:foreground="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/white"
它在 API +21 上显示带有颜色的波纹效果,在 API -21 上显示简单的灰色背景。 添加此样式:
<style name="AppTheme.MyRipple">
<item name="colorControlHighlight">@color/your_color</item>
<item name="android:background">?selectableItemBackgroundBorderless</item>
</style>
并将其设置为视图:
<Button
...
android:theme="@style/AppTheme.MyRipple" />
这段代码对我有用,可以产生涟漪:
public static void setRippleDrawable(View view, int normalColor, int touchColor) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
RippleDrawable rippleDrawable = new RippleDrawable(ColorStateList.valueOf(touchColor), view.getBackground(), null);
view.setBackground(rippleDrawable);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(touchColor));
stateListDrawable.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(touchColor));
stateListDrawable.addState(new int[]{}, new ColorDrawable(normalColor));
view.setBackground(stateListDrawable);
}
} catch (Exception e) {
Log.e(LOG_TAG, "" + e);
}
}
我没有找到修改 selectableItemBackground 属性的方法。 这就是为什么我像上面那样做。
在深黑色主题(或任何其他)应用程序中尝试使用如下
首先在 drawable
文件夹中创建 ripple_effect.xml
并添加代码
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#f5f5f5">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f5f5f5" />
</shape>
</item>
</ripple>
然后将背景设置为您的任意视图,例如 Linear layout, Button, TextView
等
<TextView
android:id="@+id/tvApply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect"
android:clickable="true"
android:text="APPLY"
android:textColor="#FFFFFF"
android:gravity="center"
android:textSize="@dimen/_8sdp"
android:padding="@dimen/_8sdp"
android:focusable="true" />
使用以下步骤:
1. Make changes to button view in your layout.xml
2. Add new styles in styles.xml
your_layout.xml
<Button
android:id="@+id/setup_submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_in"
android:textStyle="bold"
android:background="@color/colorPrimary"
android:textColor="@color/white"
style="@style/SelectableItemBackground"
android:foreground="?android:attr/selectableItemBackground"/>
-The style attribute calls the style that we created.
-Foreground attribute calls the andorid's default selectable attribute.
styles.xml
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/white</item>
</style>
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>