[英]How to change the spinner text color while spinner set on the toolbar in android?
[英]How to set the items' text color in Spinner Android
我在為Spinner設置文本顏色時遇到問題。 我見過幾個例子,但是大多數都有來自res folder
strings.xml
ArrayAdapter和String數組,因為我的Spinner的項目是從SQLite數據庫中檢索的,所以我認為它可能沒有幫助。
這是我的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();
而且,這些是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" />
請幫我講解如何在Spinner中設置項目文本顏色。
我將不勝感激任何幫助。 謝謝。! =)
為您的微調器項創建一個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"
/>
最后在你的代碼中。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_view,yourList);
使用Custom ArrayAdapter或Custom Spinner。
修改文本顏色在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"/>
和調用適配器類似這樣:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array,
R.layout.spinner_text);
這是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"/>
這里的問題是?android:attr / dropdownListPreferredItemHeight不公開,但它似乎是48 dip或64dip: https ://android.googlesource.com/platform/frameworks/base/+/414c4984fdbb03b688bb5c3c76d20100fce3d067%5E1..414c4984fdbb03b688bb5c3c76d20100fce3d067/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.