[英]How can i add divider to my drop down list (spinner)?
我想在我的下拉列表中添加分隔线。
我使用了在 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>
这仅适用于 spinnerMode="dropdown"... 对于对话模式,必须在运行时通过适配器添加分隔符(引用的示例也使用下拉列表,但在实现它并将模式更改为对话框后,分隔符仍在显示)。
只需尝试将其添加到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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.