简体   繁体   English

如何更改textView android.R.layout.simple_spinner_dropdown_item的颜色?

[英]How can I change the color of the textView android.R.layout.simple_spinner_dropdown_item?

I'm creating a SpinnerAdapter using the built in resource ID android.R.layout.simple_spinner_dropdown_item 我正在使用内置资源ID android.R.layout.simple_spinner_dropdown_item创建一个SpinnerAdapter

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item);

but the textView which that resource ID creates has textColor Black and I need a different color. 但资源ID创建的textView有textColor Black,我需要不同的颜色。 What's the best way of changing the color but keeping everything else the same? 改变颜色但保持其他一切的最佳方法是什么?

When I try and create my own textView layout resource in an xml file, eg 当我尝试在xml文件中创建自己的textView布局资源时,例如

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/White"
    />

then it doesn't behave in the same way as android.R.layout.simple_spinner_dropdown_item because eg the padding is different. 然后它的行为与android.R.layout.simple_spinner_dropdown_item的行为不同,因为例如填充不同。 So 所以

  1. Is there a way of creating my own layout resource in an xml file which inherits everything from android.R.layout.simple_spinner_dropdown_item and allows me to override the textColor? 有没有办法在xml文件中创建自己的布局资源,该文件继承了android.R.layout.simple_spinner_dropdown_item中的所有内容,并允许我覆盖textColor?
  2. Or is the complete definition of android.R.layout.simple_spinner_dropdown_item available somewhere? 或者是某个地方可以找到android.R.layout.simple_spinner_dropdown_item的完整定义?
  3. Or perhaps there's an even easier way somehow? 或者或许有一种更简单的方式?

This question relates to another question that I've asked today ( Can't change the text color with Android Action Bar drop-down navigation ). 这个问题涉及我今天提出的另一个问题( 无法使用Android Action Bar下拉导航更改文本颜色 )。 I've realised that one answer to that question (and hence this question) is to create my own ArrayAdapter class which inherits from ArrayAdapter<T> so that I can always set the color in code whenever the ArrayAdapter gets used (see my answer to that question). 我已经意识到这个问题的一个答案(因此这个问题)是创建我自己的ArrayAdapter类,它继承自ArrayAdapter<T>这样我可以随时在使用ArrayAdapter时在代码中设置颜色(参见我的答案)那个问题)。 But that seems like a very cumbersome solution :-|. 但这似乎是一个非常麻烦的解决方案: - |。

Changing a text color shouldn't be a hard task! 改变文字颜色应该不是一项艰巨的任务!

To expand on Sam's solution, you will need to modify the file in very specific ways to override Android's styles. 要扩展Sam的解决方案,您需要以非常具体的方式修改文件以覆盖Android的样式。

After you copy the simple_spinner_dropdown_item.xml from the sdk folder into the correct layout folder you are working with, you will see this code: simple_spinner_dropdown_item.xml从sdk文件夹复制到正在使用的正确布局文件夹后,您将看到以下代码:

<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/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />

In order to change the background of the TextView and avoid this error: 为了更改TextView的背景并避免此错误:

.../res/layout/simple_spinner_dropdown_item.xml:20: error: Error: Attribute is not public. ... / res / layout / simple_spinner_dropdown_item.xml:20:错误:错误:属性不公开。 (at 'layout_height' with value '?android:attr/dropdownListPreferredItemHeight'). (在'layout_height'中,值为'?android:attr / dropdownListPreferredItemHeight')。

your code will need to look like this: 您的代码需要如下所示:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/spinnerText"
style="@style/SpinnerTextViewItem"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

The other main change that needs to be made is to create your own style. 需要做的另一个主要变化是创建自己的风格。 You probably have a styles.xml file, so add a new style similar to this: 您可能有一个styles.xml文件,因此添加一个类似于此的新样式:

<style name="SpinnerTextViewItem" parent="@android:style/Widget.TextView" >
    <item name="android:textStyle">normal</item>
    <item name="android:background">@color/green</item>
</style>

Assuming you have something in your colors.xml file, you can set the color via "@color/green" or whatever color you choose (of course you can use @android:color/...) not to mention all the other items you can add to change the TextView. 假设你的colors.xml文件中有东西,你可以通过“@ color / green”或你选择的任何颜色设置颜色(当然你可以使用@android:color / ...)更不用说所有其他项目了您可以添加以更改TextView。

Is there a way of creating my own layout resource in an xml file which inherits everything from android.R.layout.simple_spinner_dropdown_item and allows me to override the textColor? 有没有办法在xml文件中创建自己的布局资源,该文件继承了android.R.layout.simple_spinner_dropdown_item中的所有内容,并允许我覆盖textColor?

No, but you can cut & paste your favorite version of simple_spinner_dropdown_item.xml to a new file then simply change the text color. 不,但您可以将喜爱的simple_spinner_dropdown_item.xml版本剪切并粘贴到新文件中,然后只需更改文本颜色即可。

Or is the complete definition of android.R.layout.simple_spinner_dropdown_item available somewhere? 或者是某个地方可以找到android.R.layout.simple_spinner_dropdown_item的完整定义?

Yes, but there are many different versions. 是的,但有许多不同的版本。 Which API do you like? 您喜欢哪种API? Also you might have a copy on your hard drive already, check <android-sdk>/platforms/android-xx/data/res/layout/ (where xx is a particular API.) 你也可能已经在你的硬盘上有一个副本,检查<android-sdk>/platforms/android-xx/data/res/layout/ (其中xx是一个特定的API。)


As an alternate approach you can create a custom Adapter and change the text color after you inflate each row, but the method above will be slightly faster since it doesn't involve any run time changes. 作为替代方法,您可以创建自定义适配器并在每行充气后更改文本颜色,但上面的方法会稍微快一些,因为它不涉及任何运行时更改。

Without using custom adapter it is impossible to change the color of 如果不使用自定义适配器,则无法更改颜色

 android.R.layout.simple_spinner_dropdown_item

Better create a custom adapter and then you can change the color, testSixe, textStyle, etc. 更好地创建自定义适配器,然后您可以更改颜色,testSixe,textStyle等。

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

相关问题 如何将android.R.layout.simple_spinner_dropdown_item设置为白色字体? - how to set my android.R.layout.simple_spinner_dropdown_item to a white font? android.R.layout.simple_spinner_dropdown_item 和 android.R.layout.simple_spinner_item 的区别 - Difference between android.R.layout.simple_spinner_dropdown_item and android.R.layout.simple_spinner_item 更改使用android.R.layout.simple_spinner_item的微调框的颜色 - Change color of a spinner that use android.R.layout.simple_spinner_item 更改android.R.layout.simple_list_item_2中的文本颜色 - Change text color in android.R.layout.simple_list_item_2 如何改变android.R.layout.simple_list_item_1的文本颜色? - How to change text color of android.R.layout.simple_list_item_1? 我可以更改Android DatePicker(模式-微调器)所选项目的颜色吗? - Can I change color of Android DatePicker (Mode - Spinner) selected item? Android 简单微调器下拉项 - Android Simple Spinner Dropdown Item Android:在下拉列表中更改微调器项目的文本颜色 - Android : Change the text color of a spinner item inside the dropdown list 如何在数字选择器和微调器中更改所选项目的颜色 - How can i change color of selected item in number picker and spinner 如何更改微调器的项目文本颜色? - How can I change Spinner's item text color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM