我似乎无法在Spinner上启用按下状态的涟漪效果。单独的下拉项目具有涟漪效果,但Spinner本身没有。我认为这可能是预期的行为,但来自材料设计指南的动画清楚地显示了选项下拉之前Spinner具有按下状态的涟漪效果。
我似乎无法在Spinner上启用按下状态的涟漪效果。单独的下拉项目具有涟漪效果,但Spinner本身没有。我认为这可能是预期的行为,但来自材料设计指南的动画清楚地显示了选项下拉之前Spinner具有按下状态的涟漪效果。
Activity
中创建了一个新的Spinner
,就像你描述的那样,使用v7兼容库或在Android 5.0+上没有应用RippleDrawable
。对我来说这并不令人惊讶;谷歌经常无法实现自己的设计。RippleDrawable
,然后使用spinner.setBackgroundResource(R.drawable.spinner_background);
将其设置为Spinner
的背景。我相信你也可以在主题中设置这个。我一直在使用这个很酷的库来为一些视图应用涟漪效果。我喜欢它,因为你可以在XML文件中包装任何视图,并应用涟漪效果。
https://github.com/balysv/material-ripple
希望能有所帮助。
在xml中设置这些Spinner属性
android:background="@drawable/ripple_effect"
android:dropDownSelector="@drawable/ripple_effect"
而ripple_effect.xml文件将会看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item android:id="@android:id/mask">
<shape android:shape="rectangle" >
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
在Github上有一个库,即Material Ripple Layout。该库支持早期版本的Android系统和Lollipop设备。此外,您可以创建自己的颜色来实现涟漪效果。以下是使用方法:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
MaterialRippleLayout.on(spinner)
.rippleColor(Color.BLACK)
.create();
但我不使用那个库。我正在使用在Support Library rev 23.0.1中引入的Spinner
默认涟漪,通过在app
模块的build.gradle
中编译:compile 'com.android.support:appcompat-v7:23.0.1'
。
因此,这是我完整的Spinner
布局代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="@dimen/dropDown_spinner"
style="@style/SpinnerStyle"/> <!-- apply the ripple style -->
</RelativeLayout>
样式定义在res/values/styles.xml
中:
<style name="SpinnerStyle" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
<item name="android:background">?android:selectableItemBackground</item>
<item name="android:dropDownSelector">?android:selectableItemBackground</item>
<item name="android:divider">@null</item>
<item name="overlapAnchor">true</item>
</style>
之后,涟漪效果应该按预期工作。
ripple_spinner.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<ripple android:color="?android:attr/colorControlHighlight">
<item>
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
</ripple>
</item>
你的 XML:
your_acitivity.xml
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownSelector="@drawable/ripple_spinner"
android:popupBackground="@drawable/popup_spinner_item_background"
/>
以下将更改下拉菜单的背景。
popup_spinner_item_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@android:color/white" />
</shape>
styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light">
<!--Workaround for the Android bug-->
<item name="android:dropDownListViewStyle">@style/Theme.MyListView</item>
</style>
<style name="Theme.MyListView" parent="@android:style/Widget.Material.ListView.DropDown">
<item name="android:listSelector">@drawable/spinner_ripple</item>
</style>
</resources>
Spinner没有涟漪效果,因为背景不可涟漪。我在selectableItemBackgroundBorderless
布局中进行了如下设置(https://developer.android.com/training/material/animations.html#Touch):
一个0
宽度的Spinner
,并删除向下箭头背景(使用@null
即可),即使我删除普通Spinner的背景,它也没有涟漪效果,所以我像Google日历一样使用TextView和ImageView。
一个TextView
+ImageView
(用于向下箭头)
当用户点击TextView/ImageView时,它会有涟漪动画,然后调用Spinner.performClick
。我希望它像正常的下拉菜单一样,所以我制作了一个0
宽度的Spinner,因为gone
Spinner不会调用onItemSelected
。
对于隐藏的适配器,可能是这样的:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
((TextView) view).setText("");
return view;
}
布局点击监听器:
layout.setOnClickListener(() -> {
spinner.performClick();
});
对于Spinner项目监听器:
spinner.setOnItemSelectedListener(() -> {
// this requires spinner's visiblity is not gone
});