Android下拉框按钮本身没有涟漪效果,只有其选项会有。

5

你能发一下你到目前为止尝试过的内容吗?也许这个链接可以帮到你:https://dev59.com/ql4c5IYBdhLWcg3weqa9 - bonnyz
6个回答

2
我在Activity中创建了一个新的Spinner,就像你描述的那样,使用v7兼容库或在Android 5.0+上没有应用RippleDrawable。对我来说这并不令人惊讶;谷歌经常无法实现自己的设计。
为了产生效果,我所做的是在res/values-v21/中创建一个RippleDrawable,然后使用spinner.setBackgroundResource(R.drawable.spinner_background);将其设置为Spinner的背景。我相信你也可以在主题中设置这个。

我开始觉得 Google 故意没有实现自己的设计,因为弹出/下拉菜单出现得太快了。这对我来说仍然非常令人沮丧,但也许只有我一个人真正受到了困扰。将背景设置为此会删除具有按下状态和着色指示器的现有背景。它也不向后兼容。 - Darren Hinderer

0

我一直在使用这个很酷的库来为一些视图应用涟漪效果。我喜欢它,因为你可以在XML文件中包装任何视图,并应用涟漪效果。

https://github.com/balysv/material-ripple

希望能有所帮助。


抱歉,我不想使用第三方库,我只想正确地为这个组件设置主题。 - Darren Hinderer

0

在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>

将背景设置为这个会移除已存在的带有按下状态和色调指示器的背景。它也不向后兼容。 - Darren Hinderer

0

在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>

之后,涟漪效果应该按预期工作。


将背景设置为此会删除具有按下状态和染色指示符的现有背景。 - Darren Hinderer

0
希望下面的代码能让你对Spinner上的涟漪效果有所了解。

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>

在你的res/values/styles.xml文件中:-

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>

希望这能对你有所帮助。

弹出/下拉菜单已经正常工作。我想要修复的是旋转器本身的按下状态。 - Darren Hinderer

0

Spinner没有涟漪效果,因为背景不可涟漪。我在selectableItemBackgroundBorderless布局中进行了如下设置(https://developer.android.com/training/material/animations.html#Touch):

  1. 一个0宽度的Spinner,并删除向下箭头背景(使用@null即可),即使我删除普通Spinner的背景,它也没有涟漪效果,所以我像Google日历一样使用TextView和ImageView。

  2. 一个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
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接