如何将divider添加到我的下拉列表(微调器)?

问题描述 投票:-1回答:1

before click

after click - i'd like to add such blue line

我想在我的下拉列表中添加divider。

我使用了我在stackoverflow上找到的解决方案,但它们没有用。

这是我在xml片段中的xml代码

<Spinner
     android:id="@+id/spinner1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:spinnerMode="dialog"
     android:background="@drawable/spinner">

</Spinner>

这是spinner.xml。它被定义为边界,形状和图像(“点击按钮”)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <layer-list>

            <item>
                <shape android:shape="rectangle">
                    <padding
                        android:left="0dp"
                        android:top="0dp"
                        android:right="0dp"
                        android:bottom="1.5dp"
                        />

                    <gradient android:startColor="@color/white" android:endColor="@color/white" android:angle="270" />
                    <stroke android:width="2px" android:color="@color/colorPrimary2" />
                    <corners android:radius="0dp" />
                </shape>
            </item>

                <item android:gravity="center|right" android:drawable="@drawable/ic_spinner_drop_down"/>



        </layer-list>
    </item>
</selector>

java android xml spinner android-spinner
1个回答
0
投票

这仅适用于spinnerMode =“dropdown”...对于对话框模式,分隔符必须是added during runtime via an adapter(引用的示例也使用下拉列表,但在实现它并将模式更改为对话框后,仍然显示分隔符)。

只需尝试将其添加到styles.xml资源目录中的values文件中:

    <style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
        <item name="android:divider">#0000ff</item>
        <item name="android:dividerHeight">0.5dp</item>
    </style>

    <style name="SpinnerTheme" parent="AppTheme">
        <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
    </style>

然后,您可以将额外的子节点添加到该文件中已存在的style标记中(将样式应用于所有微调器):

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- ... -->
        <!-- ... -->
        <!-- ... -->
        <!-- ... some existing lines -->

        <!-- ... new line to add:-->
        <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
    </style>

或者......您可以将该样式添加到片段XML中的特定Spinner标记中(将样式仅应用于此微调器):

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"
        android:background="@drawable/spinner"
        android:theme="@style/SpinnerTheme">
    </Spinner>
© www.soinside.com 2019 - 2024. All rights reserved.