我正在使用带有分隔线的ListView和ExpandedView,可以设置分隔线。但是在Spinner中似乎没有项目之间的分隔线。
有人有解决这个问题的想法吗?
我正在使用带有分隔线的ListView和ExpandedView,可以设置分隔线。但是在Spinner中似乎没有项目之间的分隔线。
有人有解决这个问题的想法吗?
这对我有用:
<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#d1d1d1</item>
<item name="android:dividerHeight">0.5dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
使用这种方法的优点是它不会消除悬停时的涟漪效果。我找到了一个更加适宜的解决方案来处理这个问题(不需要在单个布局中包含分隔符)。
你需要在你的Activity主题中进行定义。
<item name="android:dropDownListViewStyle">@style/App.Style.Spinner</item>
然后使用适当的样式创建
<style name="App.Style.Spinner" parent="@style/Widget.Sherlock.Light.ListView.DropDown">
<item name="android:dividerHeight">10dip</item>
<item name="android:divider">@drawable/mydivider</item>
</style>
<item name="android:dropDownListViewStyle">@style/App.Style.Spinner</item>
你所说的“在你的活动主题中”是什么意思?我有点困惑。 - Ojonugwa Jude Ochalifu根据@Talihawk的回答,我成功地将其应用于特定的微调框。不要设置你的活动主题,而是直接为微调框视图设置主题:
基于 @Talihawk 的回答,我修改了代码,使其仅适用于特定的下拉列表。不要设置您的活动主题,而是直接为下拉列表视图设置主题:
<style name="MatchSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
<item name="android:divider">#123456</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="MatchSpinnerTheme" parent="AppTheme">
<item name="android:dropDownListViewStyle">@style/MatchSpinnerStyle</item>
</style>
和
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/MatchSpinnerTheme"/>
drawable
中定义您的分隔符并引用它,而不是使用#123456
。还有,必须使用android:theme
而不是stlye="@stye/..."
。 - Sevastyan Savanyuk <item name="android:dropDownListViewStyle">@style/MySpinner</item>
<style name="MySpinner" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:dividerHeight">2dp</item>
<item name="android:divider">#000</item>
</style>
对于遇到相同问题的人,我差点放弃了,但是我想到了如何获取分隔符。
我在每个项目的自定义布局底部添加了分隔线。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem2">
<TextView android:id="@+id/Text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
style="@style/SpinnerView_Text" android:paddingLeft="10dip" />
<ImageView android:id="@+id/icon" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/arrowright"
android:layout_alignParentRight="true" android:layout_centerInParent="true"
android:layout_marginRight="20dip" />
</RelativeLayout>
<ImageView android:id="@+id/Divider1" android:layout_width="fill_parent"
android:layout_height="1dip" style="@style/Divider"></ImageView>
其他解决方案对我都没有用,所以我使用了一个可绘制对象作为Spinner项目的背景,以产生所需的效果。
我创建了一个新的可绘制对象 dropdown_divider.xml
和一个自定义的SpinnerAdapter类,在其中适配了getDropDownView()
方法,将背景设置为Spinner项目。
android:bottom="-56dp"
在可绘制对象中是将线条完美地居中于两个项目之间,但这取决于您在布局中应用的确切边距和填充。
dropdown_divider.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="16dp"
android:right="16dp"
android:bottom="-56dp">
<shape android:shape="line" >
<stroke
android:width=".2dp"
android:color="#FF666666" />
</shape>
</item>
</layer-list>
SpinnerAdapter
:
@Override
public View getDropDownView(int position, View convertView,
@NonNull ViewGroup parent) {
TextView text = (TextView) super.getDropDownView(position, convertView, parent);
text.setBackground(context.getDrawable(R.drawable.dropdown_divider));
label.setTextColor(Color.BLACK);
label.setText(lists.get(position).getTitle());
return text;
}