更改微调编程风格 [英] Change Spinner style programmatically

查看:195
本文介绍了更改微调编程风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现改变应用程序的主题了按钮的点击功能。
我已经成功地改变了风格的按钮,edittexts和textviews。但面对不断变化的风格微调的问题。

I need to implement function of changing the application theme up on button click. I have already changed successfully style of buttons, edittexts and textviews. But faced the problem of changing style for Spinner.

唯一成功的改变是背景颜色的变化:

The only successful change is change of background color:

spinnerRoutes.setBackgroundResource(R.drawable.dark_theme_spinner_background);

BUT 仅标题行背景的改变。我还需要更改行和项的文字颜色,和微调行的背景。

BUT only background of title row changed. I need also to change textColor of rows and items, and background of spinner rows.

我已经尝试过类似计算器的解决方案很多,但没有一次成功。

I have already tried alot of similar stackoverflow solutions but none of them worked.

它甚至有可能改变微调的编程风格?

推荐答案

其他的人是正确的,当涉及到完全定制的微调的项目。
但是,如果你需要改变的唯一事情是在项目的文字(这意味着你仍然想要一个文本视图但说不同的字体,颜色,重力),那么你只需要通过转接器自定义项目布局的样式:

The other guys are right when it comes to fully customizing the items in the spinner. But if the only thing you need to change is the style of the text in the items (meaning you still want a text view but with say different font, color, gravity) then you only need to pass the adapter a custom item layout:

这是您的适配器应该怎么是这样的:

This is how your adapter should look like:

spinner.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.custom_item, data)

其中custom_item是您创建如下一个xml布局文件:

where custom_item is an xml layout file that you create as follow:

    <?xml version="1.0" encoding="utf-8"?>
    <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:textAppearance="?android:attr/textAppearanceListItemSmall"
         android:gravity="center"
         android:textColor="@android:color/red"
         android:paddingStart="?android:attr/listPreferredItemPaddingStart"
         android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
         android:minHeight="?android:attr/listPreferredItemHeightSmall" />

只要确保文本视图是在XML独一无二的观点,并认为它的id是上面指定的,所以默认的适配器可以识别它。

Just make sure that the text view is the one and only view in that xml, and that its id is as specified above, so the default adapter can recognize it.

希望这有助于。

这篇关于更改微调编程风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆