在我的Android应用程序中,我正在使用旋转器,我已经从SQLite数据库加载数据到旋转器中,它工作正常。这是它的代码。
Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();
list.add("All Lists");
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
list.add(keyList[i]);
cursor.moveToNext();
}
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
现在我想要改变旋转数据的文本颜色和文本大小。我在XML文件上使用了以下XML行作为旋转标记,但它不起作用。
android:textColor="@android:color/white"
android:textSize="11dp"
我如何改变我的转轮的文本颜色和文本大小?
Ashraf的另一个解决方案是确保你考虑到屏幕尺寸。你需要在onCreate中获得旋转器,并在设置适配器后设置监听器:
//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);
然后你可以开始改变在旋转器被点击之前显示的视图的文本大小:
private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
你所需要做的就是创建如下布局的特定文件夹:
values-sw360dp
values-sw600dp
values-sw800dp
然后在每个文件夹中添加一个名为“bol . XML”的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLargeTablet">false</bool>
</resources>
你可以有这种类型的转轮适配器,完全定制:
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#676767"));
((TextView) v).setTypeface(vrFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTypeface(vrFont);
((TextView) v).setTextColor(Color.parseColor("#676767"));
if (position == 0) {
((TextView) v).setTextColor(Color.parseColor("#979797"));
}
return v;
}
而出来。spinner_text是:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="@color/whiteThree" />