【问题标题】:How can i add divider to my drop down list (spinner)?如何将分隔线添加到我的下拉列表(微调器)?
【发布时间】:2019-03-29 00:06:20
【问题描述】:

before click

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

我想在我的下拉列表中添加分隔符。

我使用了在 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。它定义了("click-button")的边框、形状和图像

<?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】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2015-06-19
    相关资源
    最近更新 更多