【问题标题】:How to set the item text color to white of Spinner in Android如何在Android中将项目文本颜色设置为Spinner的白色
【发布时间】:2014-11-25 04:17:16
【问题描述】:

我正在开发演示应用程序,其中我使用Spinner 并且具有类似黑色的背景颜色,因此 Spinner 选择的项目颜色默认也是黑色的,因此它不可见。

我想通过 xml 或更新样式将此颜色更改为白色,以便在黑色上可见。

<Spinner
    android:id="@+id/countrySpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:hint="@string/country" />

有什么办法吗?提前致谢。

【问题讨论】:

  • 用 parent="@android:style/Widget.Holo.Dark.ProgressBar" 设置样式
  • 您是否要仅将所选项目上的文本设为白色?
  • @CSmith 你怎么解释?

标签: android spinner android-spinner


【解决方案1】:

尝试在您的 OnItemSelectedListener 中处理此问题。我认为以下应该有效。它获取所选项目的视图,在其中找到 TextView(Spinner 视图有一个 id 为 text1 的 TextView 子视图),并设置其颜色。

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View view,
                int position, long id) {

            TextView tmpView = (TextView) mySpinner.getSelectedView().findViewById(android.R.id.text1);
            tmpView.setTextColor(Color.WHITE);
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // do stuff

        }
    });

【讨论】:

    【解决方案2】:

    在你的 res/values/styles.xml 中设置一个样式:

    <style name="mySpinner" parent="@android:style/Widget.Holo.Dark.ProgressBar" />
    

    并在您的 Spinner 上引用此样式:

    <Spinner style="@style/mySpinner" ... />
    

    父样式 Widget.Holo.Dark.ProgressBar 应该为您提供一个适合深色背景样式的微调器。

    有关样式的信息,请参阅https://developer.android.com/guide/topics/resources/style-resource.html。您可以浏览 SDK res/ 文件夹以了解内置样式。

    【讨论】:

    • 我正在使用支持库 appcompat。现在我无法解决 @android:style/Widget.Holo.Dark.ProgressBar
    • 请注意,我不希望像我在问题中添加的那样。我只是希望选定的文本颜色为白色。
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多