yww*_*ynm 86 android android-appcompat android-layout material-design android-5.0-lollipop
我已经看到了一些SO问题,他们提供了一些可能的方法来实现我想要的.例如:
colorControlHighlight在styles.xml中使用属性.
这是我的styles-v21.xml:
<style name="SelectableItemBackground">
<item name="android:colorControlHighlight">#5677FC</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我的小部件:
<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"/>
Run Code Online (Sandbox Code Playgroud)
它不起作用.我还尝试添加parent="Theme.AppCompat到"SelectableItemBackground"样式,或者更改为colorControlHighlight(no android: prefix)"或更改为?android:attr/selectableItemBackground,既没有用.
backgroundTint在布局中使用属性.
所以我加入android:backgroundTint="#5677FC"我的TextView.还是没用.然后我试着改变android:backgroundTintMode,src_in并且src_atop,他们永远不会有所作为.
那么,当我?attr/selectableItemBackground用作背景时,如何改变波纹颜色.我只关注棒棒糖及以上.先感谢您!
yww*_*ynm 122
最后我找到了解决方案:我不应该android:colorControlHighlight直接在主题中使用SelectableItemBackground,而应该编写另一种风格:
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/ripple_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后:
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)
最后添加style="@style/SelectableItemBackground"到Viewlayout.xml中.
更新于2016/8/26
N发布后,我发现有时我们无法使用此方法为某种类型设置波纹颜色View(例如,CardView).现在我强烈建议开发人员使用RippleDrawable,也可以在xml中声明.这是一个例子:
我想在用户触摸/点击CardView上面的API21 时显示涟漪效应,当然在Lollipop之前应该有另一种反馈.所以我应该写:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/selectable_item_background"/>
Run Code Online (Sandbox Code Playgroud)
并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>
Run Code Online (Sandbox Code Playgroud)
selectable_item_background在drawable-v21文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ripple_black" />
</selector>
Run Code Online (Sandbox Code Playgroud)
最后,ripple_black在drawable(或drawable-v21)文件夹中:
<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-->
Run Code Online (Sandbox Code Playgroud)
而已.对于其他观点,也许你应该使用 android:background="@drawable/selectable_item_background".不要忘了设置OnClickListener,OnTouchListener或像那些对他们来说,否则输出纹波不会显示.
Kir*_*zin 54
哈兰和流亭是对的.接受的答案不是最好的方法.让我在代码中展示如何更改前Lollipop版本及更高版本的纹波颜色
你的AppTheme应该继承任何AppCompat主题并包含colorControlHighlight属性(不带'android:'前缀)
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorControlHighlight">#40ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)您的视图应包含clickable ="true"(或应该以编程方式设置单击侦听器),背景应为"?attr/selectableItemBackgroundBorderless"或"?attr/selectableItemBackground":
<LinearLayout
...
android:clickable="true"
android:background="?attr/selectableItemBackgroundBorderless"/>
Run Code Online (Sandbox Code Playgroud)注意:如果您的父视图具有白色背景,则您将看不到涟漪效果,因为它是白色的.更改另一种颜色的colorControlHighlight值
此外,如果您希望在不同活动中使用不同的纹波颜色,则可以为清单文件中的每个活动设置个人主题,例如:
<activity
android:name="com.myapp.GalleryActivity"
android:theme="@style/RedRippleTheme"
/>
Run Code Online (Sandbox Code Playgroud)
您可以在运行时更改每个片段的Activity Theme的属性.在使用自定义样式对片段进行充气之前,只需覆盖它们并应用于当前主题:
在values/styles.xml中
<style name="colorControlHighlight_blue">
<item name="colorControlHighlight">@color/main_blue_alpha26</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后,在通货膨胀之前的片段中onCreateView():
@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;
}
Run Code Online (Sandbox Code Playgroud)
此样式仅适用于此片段
您可以使用colorControlHighlight属性单独更改每个视图的波纹颜色,
如果将它们直接应用于视图,它将不起作用:
<TextView
...
colorControlHighlight="#40ffffff"/> <!-- DOESN'T WORK -->
Run Code Online (Sandbox Code Playgroud)
你应该把它作为一个主题:
<TextView
...
android:theme="@style/colorControlHighlight_blue"/>
Run Code Online (Sandbox Code Playgroud)
PS此外,有时这种方法有助于你有未知的涟漪问题,你无法搞清楚.在我的例子中,我使用了第三方滑动库,它为整个布局搞砸了涟漪效果,并明确地将这个主题添加到为我制作的所有可点击视图中.
小智 13
它在 API +21 上显示带有颜色的波纹效果,在 API -21 上显示简单的灰色背景。添加此样式:
<style name="AppTheme.MyRipple">
<item name="colorControlHighlight">@color/your_color</item>
<item name="android:background">?selectableItemBackgroundBorderless</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并将其设置为视图:
<Button
...
android:theme="@style/AppTheme.MyRipple" />
Run Code Online (Sandbox Code Playgroud)
使用以下步骤:
1. Make changes to button view in your layout.xml
2. Add new styles in styles.xml
Run Code Online (Sandbox Code Playgroud)
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"/>
Run Code Online (Sandbox Code Playgroud)
-style 属性调用我们创建的样式。
-Foreground 属性调用andorid 的默认selectable 属性。
样式文件
<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>
Run Code Online (Sandbox Code Playgroud)
接受的答案是错误的.
正确的使用方法是Liuting在评论中提到的.使用colorControlHighlight而不是android:colorControlHighlight用来改变默认colorControlHighlight从AppCompat
*请参阅主题部分中的http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html*
| 归档时间: |
|
| 查看次数: |
49264 次 |
| 最近记录: |