【问题标题】:Android Toolbar with Spinner with white text and white popup background?带有白色文本和白色弹出背景的 Spinner 的 Android 工具栏?
【发布时间】:2016-07-25 22:09:44
【问题描述】:

我一直在尝试向我的工具栏添加一个微调器,但未能实现我想要的。

这是我目前所拥有的。

正如您在屏幕截图中看到的那样,Spinner 选择的文本颜色为黑色(我希望它为白色),而弹出窗口的背景为白色,文本颜色为黑色(没关系)。

这是我的代码。

       <android.support.v7.widget.Toolbar
        android:id="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_date_range_white_24dp" />

            <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/date_range"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:entries="@array/date_range_options"
                android:spinnerMode="dropdown" />

        </LinearLayout>

    </android.support.v7.widget.Toolbar>

如果我更改工具栏并添加 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar",如下所示:

<android.support.v7.widget.Toolbar
        android:id="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_alignParentBottom="true">

我得到了微调器选定文本的白色文本颜色,但弹出背景也变暗了白色文本颜色。

我想在选定的文本颜色和带有黑色文本的白色弹出背景。

我该怎么做?

【问题讨论】:

    标签: android spinner toolbar


    【解决方案1】:

    您可以使用主题来执行此操作

    <android.support.v7.widget.AppCompatSpinner
                    android:id="@+id/date_range"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:entries="@array/date_range_options"
                    android:theme="@style/spinnerStyle
                    android:spinnerMode="dropdown" />
    

    然后在你的样式文件中

    <style name="spinnerStyle" parent="AppTheme">
        <item name="colorControlNormal">@color/white_text</item>
        <item name="colorControlActivated">@color/white_text</item>
        <item name="colorControlHighlight">@color/white_text</item>
    </style>
    

    将@color/white_text 替换为您想要使用的任何颜色。

    【讨论】:

    • 嗨。我试过你的建议,但没有奏效。它显示的效果与上面的屏幕截图中的效果相同。
    • 奇怪可能是因为它在工具栏中。这对我来说很奇怪。您是否为微调器使用自定义适配器。如果在 getView 方法中是这样,你可以这样做 TextView textView = (TextView) convertView.findViewById(android.R.id.text1); deviceName.setTextColor(ContextCompat.getColor(getContext(), R.color.white_text));假设您使用的是微调器的默认布局 (android.R.layout.simple_list_item_1)
    • 不,我没有使用适配器。这都是在 xml 上。
    • 您的目标是什么 api 级别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多