Android:无法样式微调分隔线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10916658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:27:42  来源:igfitidea点击:

Android: Cannot style spinner divider

androidthemesspinnerandroid-spinnerdivider

提问by Russ

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.

我正在尝试为我的第一个 Android 应用程序创建一个主题,它让我绕弯子。我终于设法弄清楚如何为下拉列表中的项目设置样式,但现在我无法更改列表项目之间分隔线的颜色。我在 stackoverflow 上搜索了类似的问题,并尝试了几十种组合,但似乎没有任何效果。

Here is my styles.xml file (abbreviated for clarity):

这是我的styles.xml 文件(为清楚起见而缩写):

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

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?

无论我做什么,我都会在项目之间得到一个 1dp 的浅灰色分隔线(在我的浅色列表项目背景下几乎看不到)-分隔线的高度和颜色都不会受到影响(我也尝试将其设置为可绘制,也没有效果)。我究竟做错了什么?

回答by christophercotton

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.

我有一个非常简单的 Spinner Activity,它适用于以下情况。我看到的唯一区别是你有一个,<item name="android:height">3dp</item>而我根本没有。

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

and in my Activity I have:

在我的活动中,我有:

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

and then for the main layout I have the following XML:

然后对于主布局,我有以下 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>

Here is the screenshot

这是屏幕截图

sample activity screnshot

示例活动截图

If you can't get it to work from there, I can push up the entire thing to a github repo for you.

如果您无法从那里开始工作,我可以为您将整个内容推送到 github 存储库。

回答by Barak

You could add a horizontal line to the dropdown layout you use, which would effectively create a divider.

您可以在您使用的下拉布局中添加一条水平线,这将有效地创建一个分隔线。

EDIT

编辑

Some further searching found this:

一些进一步的搜索发现了这一点:

SO Answer

所以答案

Which basically says what you show you are trying to do above should work... although it mentions setting that style in your activitytheme and you don't mention doing that.

这基本上说明了您在上面展示的内容应该可以工作......尽管它提到在您的活动主题中设置该样式并且您没有提到这样做。

回答by Cristofer

You can do it in your layout.xml

你可以在你的 layout.xml 中做到

     <Spinner
                android:id="@+id/sp_to_create"
                android:layout_width="match_parent"
                android:layout_height="32dp"
                android:layout_marginBottom="10dp"
                style="@style/spinner_style"
                android:prompt="@string/to_type_prompt" />

XML STYLES ADD it

XML 样式 添加它

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

Add to your Activity Theme

添加到您的活动主题

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="android:dropDownListViewStyle">@style/spinner_style</item>
</style>

JAVA FILE

爪哇文件

your_spinnerList.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_dropdown_item_1line, timeOff_type_list));

Let me know if it had been useful for you! Have a nice day!

如果它对您有用,请告诉我!祝你今天过得愉快!

回答by Oren

The style method in the accepted answer works well until you need two spinners with different divider colors.

接受的答案中的样式方法效果很好,直到您需要两个具有不同分隔线颜色的微调器。

Here is what I found works as an alternative:

这是我发现的替代方法:

a) Set the popupBackgroundColor attribute on the spinner to the color you want for the divider. This will color the entire list item's background (including the space we think of as the divider).

a) 将微调器上的 popupBackgroundColor 属性设置为您想要的分隔线颜色。这将为整个列表项的背景着色(包括我们认为是分隔线的空间)。

b) Set the spinners adapters dropDownViewResource to be a CheckedTextView with it's background attribute set to some other color (or a selector if you want the selected items to have a different color). This will override the color we set in step a for everything but the divider. effectively giving us the desired result.

b) 将 Spinners 适配器 dropDownViewResource 设置为 CheckedTextView,其背景属性设置为其他颜色(如果您希望所选项目具有不同的颜色,则设置为选择器)。这将覆盖我们在步骤 a 中为除分隔线以外的所有内容设置的颜色。有效地给我们想要的结果。

So you will have:

所以你将拥有:

drawable/spinner_dropdown_background_selector:

drawable/spinner_dropdown_background_selector:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/your_unchecked_color" android:state_checked="false"/>
    <item android:drawable="@color/your_checked_color" android:state_checked="true"/>
    <item android:drawable="@color/your_unchecked_color"/>

</selector>

layout/drop_down_item.xml:

布局/drop_down_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@android:id/text1"
             android:background="@drawable/spinner_dropdown_background_selector"
             android:textColor="@android:color/white"
             android:singleLine="true"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:ellipsize="marquee" />

Your spinner definition:

您的微调器定义:

<Spinner
        ...
        android:popupBackground="@color/your_divider_color"            
        ...
        />

And finally your array adapter definition:

最后你的数组适配器定义:

ArrayAdapter<String> dataAdapter = new ...
dataAdapter.setDropDownViewResource(android.R.layout.drop_down_item);
spinner.setAdapter(dataAdapter);

Please note that setting the popupBackgroundColor has no effect if the spinner is in dialog mode.

请注意,如果微调器处于对话模式,则设置 popupBackgroundColor 无效。