简体   繁体   English

spinnerDropDownItemStyle的Android微调器自定义样式无法正常工作

[英]Android spinner custom style for spinnerDropDownItemStyle not working

I have added some custom style to Android spinner. 我已经为Android微调器添加了一些自定义样式。 I am trying to have prompt small but drop down items little big. 我试图提示小但下拉项目有点大。 My code looks like below : 我的代码如下所示:

theme.xml theme.xml

<style name="Theme.MyApp" parent="@android:style/Theme.Holo.Light.DarkActionBar">
.
.
.
 <item name="android:spinnerItemStyle">@style/spinnerItemStyle</item>
 <item name="android:spinnerDropDownItemStyle">@style/spinnerDropDownItemStyle</item>
</style>
.
.
.
<style name="spinnerItemStyle">
        <item name="android:padding">10dp</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#000000</item>
</style>
<style name="spinnerDropDownItemStyle">
        <item name="android:padding">20dp</item>
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#000000</item>
 </style>

Now I am able to set the spinnerItemStyle properly, but some how style for spinnerDropDownItemStyle is not having any effect, its not working. 现在我可以正确设置spinnerItemStyle ,但有些spinnerDropDownItemStyle样式没有任何效果,它不起作用。 Any clues why is this happening ? 任何线索为什么会发生这种情况? I want my drop-down items with big text size than that of prompt item. 我希望我的下拉项目的文本大小大于提示项目。

This question is old but was not correct answered. 这个问题很老但是答案不正确。

Support Library has a custom support_simple_spinner_dropdown_item.xml 支持库具有自定义support_simple_spinner_dropdown_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/text1"
      style="?attr/spinnerDropDownItemStyle"
      android:singleLine="true"
      android:layout_width="match_parent"
      android:layout_height="?attr/dropdownListPreferredItemHeight"
      android:ellipsize="marquee"/>

Therefore, to make your custom style work, you must override a local spinnerDropDownItemStyle (without the android: prefix) in your app style. 因此,要使自定义样式起作用,必须在应用程序样式中覆盖本地spinnerDropDownItemStyle (不带android:前缀)。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:spinnerItemStyle">@style/TextViewSpinnerItem</item>
    <item name="android:spinnerDropDownItemStyle">@style/TextViewSpinnerDropDownItem</item>
    <!-- Override the dropdown item in support library -->
    <item name="spinnerDropDownItemStyle">@style/TextViewSpinnerDropDownItem</item>
</style>

Change whatever you want: 改变你想要的任何东西

<style name="TextViewSpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
</style>

<style name="TextViewSpinnerDropDownItem" parent="Widget.AppCompat.DropDownItem.Spinner">
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
</style>

Now, all your simple spinner dropdowns will look exactly the same. 现在,所有简单的微调器下拉列表看起来都完全相同。

Found this article that explains how to do it: How to change a Spinner text size, color or overall style 找到这篇文章解释了如何做到这一点: 如何更改Spinner文本大小,颜色或整体样式

You need to create a style for the input item and the dropdown items and add them. 您需要为输入项和下拉项创建样式并添加它们。

Customize spinner input: 自定义微调器输入:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#ff0000" />

Add style: 添加样式:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(adapter);

Customize dropdown list items: 自定义下拉列表项:

    <?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

Add style to spinner: 添加样式到微调器:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);

I know it's old question, but I recently ran into same problem. 我知道这是一个老问题,但我最近遇到了同样的问题。 The solution was to remove android: from <item name="android:spinnerDropDownItemStyle"> 解决方案是从<item name="android:spinnerDropDownItemStyle">删除android: .

According to Roman_D's comment , it has something to do with AppCompat Theme. 根据Roman_D的评论 ,它与AppCompat Theme有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM