如何在 Spinner Android 中设置项目的文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12524725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the items' text color in Spinner Android
提问by Preeyah
I'm having problems with setting text color for the Spinner. I've seen few examples but most have ArrayAdapter and String array from strings.xml
in res folder
as my Spinner's items are retrieved from SQLite Database so I think it may not help.
我在为 Spinner 设置文本颜色时遇到问题。我见过的例子很少,但大多数都有 ArrayAdapter 和 String 数组strings.xml
,res folder
因为我的 Spinner 项目是从 SQLite 数据库中检索的,所以我认为它可能无济于事。
Here are my Spinner's codes PersonalInformation.java
这是我的 Spinner 代码 PersonalInformation.java
public class PersonalInformation extends Activity
{
EditText txtLikes, txtDislikes, txtType, txtDate;
Button btnView, btnBack;
Spinner nameSpinner;
final Context context = this;
private int namesSpinnerId;
LikesDBAdapter likeDB = new LikesDBAdapter(this);
DislikesDBAdapter dlikeDB = new DislikesDBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
buddyDB.open();
Cursor friendsCursor = buddyDB.getAllNames();
startManagingCursor(friendsCursor);
String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
int[] to = new int[]{R.id.name};
SimpleCursorAdapter friendsCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, friendsCursor, from, to);
friendsCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner = (Spinner)findViewById(R.id.nameSpinner);
nameSpinner.setAdapter(friendsCursorAdapter);
//buddyDB.close();
nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Cursor c = (Cursor)parent.getItemAtPosition(pos);
namesSpinnerId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
buddyDB.close();
And, these are Spinner's layout codes in info.xml
而且,这些是 info.xml 中 Spinner 的布局代码
<Spinner
style="@style/SpinnerStyle"
android:id="@+id/nameSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/friends_prompt"
android:textColor="@color/green" />
Please help me with how to set the items text color in Spinner.
I'll appreciate any help provided. Thanks.! =)
请帮助我如何在 Spinner 中设置项目文本颜色。
我将不胜感激提供的任何帮助。谢谢。!=)
采纳答案by The Holy Coder
Use Custom ArrayAdapter or Custom Spinner.
使用自定义 ArrayAdapter 或自定义微调器。
Have you seen http://www.coderzheaven.com/2011/02/20/how-to-create-custom-layout-for-your-spinner-in-android-or-customizable-spinner-2/?
回答by V.J.
Create a xml file for your spinner item. and put it in layout folder
为微调项创建一个 xml 文件。并将其放在布局文件夹中
spinner_view.xml:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="@color/green"
/>
and finally in your code.
最后在你的代码中。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_view,yourList);
回答by jiahao
This is the source code of simple_spinner_drowdown_item.xml
这是 simple_spinner_drowdown_item.xml 的源代码
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<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"
android:textAlignment="inherit"/>
The problem here is ?android:attr/dropdownListPreferredItemHeight is not public, but it seems it is 48 dip or 64dip: https://android.googlesource.com/platform/frameworks/base/+/414c4984fdbb03b688bb5c3c76d20100fce3d067%5E1..414c4984fdbb03b688bb5c3c76d20100fce3d067/
这里的问题是 ?android:attr/dropdownListPreferredItemHeight 不是公开的,但它似乎是 48dip 或 64dip:https://android.googlesource.com/platform/frameworks/base/+/414c4984fdbb03b688bb5c3c76d20100fce3d067485dc603d067816dc603d067816dc78dc78dc78dc70300000
回答by xyz
modify the text color create a new xml file in your res/layout folder
修改文本颜色在 res/layout 文件夹中创建一个新的 xml 文件
<TextView android:id="@+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#2f4f4f"
android:textSize="20dp"
xmlns:android="http://schemas.android.com/apk/res/android"/>
and call adapter similar to like this :
并像这样调用适配器:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array,
R.layout.spinner_text);