Android 使用?attr/selectableItemBackground作为背景时,如何修改涟漪颜色?
我看到了一些这样的问题,他们给出了一些可能的方法来实现我想要的。例如:Android 使用?attr/selectableItemBackground作为背景时,如何修改涟漪颜色?,android,android-layout,android-5.0-lollipop,material-design,android-appcompat,Android,Android Layout,Android 5.0 Lollipop,Material Design,Android Appcompat,我看到了一些这样的问题,他们给出了一些可能的方法来实现我想要的。例如: <activity android:name="com.myapp.GalleryActivity" android:theme="@style/RedRippleTheme" /> 在styles.xml中使用colorControlHighlight属性 以下是我的styles-v21.xml: <style name="Selectabl
<activity
android:name="com.myapp.GalleryActivity"
android:theme="@style/RedRippleTheme"
/>
colorControlHighlight
属性
以下是我的styles-v21.xml:
<style name="SelectableItemBackground">
<item name="android:colorControlHighlight">#5677FC</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
#5677FC
?属性/可选择项背景
和我的小部件:
<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(无android:prefix)”
,或者更改为?android:attr/SelectableItemBackground
,这两种方法都没有用backgroundTint
属性
因此,我将android:backgroundTint=“#5677FC”
添加到我的TextView
。还是没用。然后,我尝试将android:backgroundTintMode更改为src_in
和src_top
,但它们从来没有改变那么,当我使用
?attr/selectableItemBackground
作为背景时,如何更改波纹颜色呢。我只关注棒棒糖及以上。提前谢谢你 最后我找到了解决方案:我应该写另一种风格,而不是直接在主题SelectableItemBackground
中使用android:colorControlHighlight
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/ripple_color</item>
</style>
和可绘制
文件夹中的可选项目\u背景
:
<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>
<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-->
最后,在drawable
(或drawable-v21
)文件夹中的ripple_black
:
就这样。对于其他视图,也许您应该使用android:background=“@drawable/Selective\u item\u background”。不要忘记为他们设置一个
OnClickListener
,OnTouchListener
或类似的设置,否则ripple不会显示。接受的答案是错误的
正确的使用方法是刘婷在评论中提到的。
使用
colorControlHighlight
而不是android:colorControlHighlight
从AppCompat
*请参阅主题部分*pre-和棒棒糖+设备的涟漪效应
哈兰和刘婷是对的。公认的答案不是最好的办法。
让我在代码中演示如何更改棒棒糖前版本及更高版本的涟漪颜色
不同视图的不同波纹颜色?(棒棒糖+) 您可以分别使用更改每个视图的涟漪颜色
colorControlHighlight
属性,如果直接将其应用于视图,则该属性不起作用:
<TextView
...
colorControlHighlight="#40ffffff"/> <!-- DOESN'T WORK -->
您应将其作为主题应用:
<TextView
...
android:theme="@style/colorControlHighlight_blue"/>
另外,如果您对ripple有未知的问题而无法解决,这种方法有时会有所帮助。
在我的例子中,我使用了第三方滑动库,它破坏了整个布局的连锁反应,并明确地将此主题添加到为我设计的所有可单击视图中。将前景属性用作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>
@颜色/你的颜色
?SelectableItemBackground无边框
并将其设置为视图:
<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\u 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>
使用以下步骤:
1. Make changes to button view in your layout.xml
2. Add new styles in styles.xml
您的_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"/>
-style属性调用我们创建的样式
-前台属性调用ANDROID的默认可选属性
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>
@颜色/白色
@样式/可选择项主题
?属性/可选择项背景
使用colorControlHighlight
而不是android:colorControlHighlight
更适合我,否则它只适用于v21+,这不是正确的答案,请参阅下面@harrane的答案。如果您没有设置ClickListener,只需将视图设置为可点击clickable=“true”
而且涟漪效果也会起作用。这样做,您将无法更改涟漪效果的颜色。问题-使用?attr/selectableItemBackground作为背景时,如何修改ripple color?
需要注意的是,它确实与您编写的android:theme
一样,而不是人们通常使用的样式。
<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);
}
}
<?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>
<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
<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"/>
<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>