Android:无法样式微调分隔线 [英] Android: Cannot style spinner divider
问题描述
我正在尝试为我的第一个 Android 应用创建一个主题,这让我不知所措.我终于设法弄清楚如何为下拉列表中的项目设置样式,但现在我无法更改列表项目之间分隔线的颜色.我在 stackoverflow 上搜索过类似的问题,尝试了几十种组合,但似乎没有任何效果.
I'm trying to create a theme for my first Android app, and it is driving me round the bend. I finally managed to figure out how to style items in a dropdown list, but now I can't change the colour of the divider between list items. I have searched similar questions on stackoverflow, and tried dozens of combinations, but nothing seems to work.
这是我的styles.xml文件(为了清楚起见而缩写):
Here is my styles.xml file (abbreviated for clarity):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:spinnerStyle">@style/spinnerStyle</item>
<item name="android:spinnerDropDownItemStyle">@style/spinnerDropDownItemStyle</item>
<item name="android:dropDownListViewStyle">@style/spinnerListViewStyle</item>
</style>
<style name="spinnerStyle" parent="@android:style/Widget.Spinner">
<item name="android:background">@drawable/my_theme_spinner</item>
</style>
<style name="spinnerDropDownItemStyle" parent="@android:style/Widget.DropDownItem.Spinner">
<item name="android:background">@drawable/my_theme_spinner_item</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:gravity">center_vertical</item>
</style>
<style name="spinnerListViewStyle" parent="@android:style/Widget.ListView.DropDown">
<item name="android:height">3dp</item>
<item name="android:dividerHeight">3dp</item>
<item name="android:divider">@color/divider</item>
</style>
</resources>
无论我做什么,我都会在项目之间得到一个 1dp 的浅灰色分隔线(在我的浅色列表项目背景下几乎看不到)——分隔线的高度和颜色都不会受到影响(我也尝试过设置它可以绘制,也没有效果).我做错了什么?
No matter what I do, I just get a 1dp light grey divider between items (which can barely be seen with my light coloured list item background) - neither the height nor colour of the divider is ever affected (I also tried setting it to a drawable, also with no effect). What am I doing wrong?
推荐答案
我有一个非常简单的 Activity 和 Spinner,它适用于以下情况.我看到的唯一区别是你有一个 <item name="android:height">3dp</item>
而我根本没有.
I have a very simple Activity with the Spinner and it works for the following. The only difference I see is that you have a <item name="android:height">3dp</item>
and I don't have that at all.
<style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
<item name="android:divider">#ff0000</item>
<item name="android:dividerHeight">5dp</item>
</style>
<style name="SampleTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">@style/TestSpinnerStyle</item>
</style>
在我的活动中,我有:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(dataAdapter);
然后对于主要布局,我有以下 XML:
and then for the main layout I have the following XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, StylingActivity"
/>
<Spinner android:id="@+id/spinner"
android:layout_width="250dp"
android:layout_height="40dp"
/>
</LinearLayout>
这是截图
如果您无法从那里开始工作,我可以为您将整个内容推送到 github 存储库.
If you can't get it to work from there, I can push up the entire thing to a github repo for you.
这篇关于Android:无法样式微调分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!