【发布时间】:2016-06-18 06:47:59
【问题描述】:
我有一个自定义微调器(简单文本视图),因为我想要一个简单的解决方案来更改所选项目的背景颜色:
spinner_categories.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="150sp"
android:layout_height="40sp"
android:textSize="16sp"
android:gravity="center" />
项目通过自定义适配器膨胀,并且背景颜色根据所选项目更改,其中颜色存储为 sharedprefs
main_acitivity
spinner = (Spinner) findViewById(R.id.spinner);
// The spinner items are populated from strings.xml, array elements
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.categories, R.layout.spinner_categories);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
// it sets the background color of the textview based on the selected item
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
sharedpref_colorCategory = getSharedPreferences(COLORS_CATEGORY, Context.MODE_PRIVATE);
parent.getChildAt(0).setBackgroundColor(sharedpref_colorCategory.getInt(spinner.getAdapter().getItem(position).toString(), 0));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
文字故意为白色,与背景形成对比。
现在,当我在三星 S5(Android 5.0 - API 21)或模拟器上安装它时,背景颜色显示正确,而华为 Y360(Android 4.4.2 - API 19)之一的背景颜色始终是白色,所以我看不到所选项目。 build.gradle 已配置如下:
build.gradle
minSdkVersion 16
targetSdkVersion 16
有人知道我该如何解决这个问题吗?
谢谢!
朱塞佩
回答: 我应该改用 setBackgroundResource 并在 colors.xml 文件中定义颜色
【问题讨论】:
-
从 Spinner 中选择项目后,您是要更改该特定项目的颜色还是要更改 Spinner 内整个文本的颜色
-
我想根据选定的微调器项目更改背景颜色...
标签: android colors background-color