更改 Android 微调器第一项背景颜色
Change Android Spinner 1st item background color
我想更改微调器的第一个下拉菜单背景颜色。可能吗?
例如,我有 3 个用于性别选择的项目菜单。 1) Select 你的性别 2) 男 3) 女。
我想在微调器下拉列表中显示这 3 个项目。我想将第一项的背景更改为浅蓝色,其他两项应为白色。可以吗
使用微调适配器怎么样?
例如,
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, testarray) {
@Override
public boolean isEnabled(int position) {
return position != 1;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView tv = (TextView) v.findViewById(R.id.spinnerTarget);
tv.setText(testarray.get(position));
switch (position) {
case 0:
tv.setTextColor(Color.RED);
break;
case 1:
tv.setTextColor(Color.BLUE);
break;
default:
tv.setTextColor(Color.BLACK);
break;
}
return v;
}
};
pSpinner.setAdapter(spinnerAdapter);
这是 simple_spinner_dropdown_item.xml 行。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14pt" />
示例代码更改文本颜色。所以你可以尝试改变背景颜色
我为适配器创建了一个单独的 class。在这里
class GenderDropDownAdapter(
private val mContext: Context,
private val genderList: ArrayList<String>
) : ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, genderList) {
override fun areAllItemsEnabled(): Boolean {
return false
}
override fun isEnabled(position: Int): Boolean {
return position != 0
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
var customView = convertView
if (customView == null) {
val mContext = this.context
val vi: LayoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
customView = vi.inflate(R.layout.item_gender_dropdown, null)
}
val genderTextView: TextView = customView?.findViewById<View>(R.id.gender_dropdown_text) as TextView
genderTextView.text = genderList[position]
if (position == 0) {
genderTextView.setBackgroundColor(mContext.resources.getColor(R.color.colorAccent))
genderTextView.setTextColor(mContext.resources.getColor(R.color.white))
} else {
genderTextView.setTextColor(mContext.resources.getColor(R.color.black))
}
return customView
}
}
并在我的 activity
中使用此适配器
class PersonalDetailActivity : BaseActivity<AuthenticationViewModel>(), AdapterView.OnItemSelectedListener {
private val genderList = arrayListOf(
"Select Gender",
"Male",
"Female"
)
override fun getLayoutResource() = R.layout.activity_personal_detail
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val genderDropDownAdapter = GenderDropDownAdapter(baseContext, genderList)
gender_spinner.adapter = genderDropDownAdapter
gender_spinner.onItemSelectedListener = this
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
gender = when (position) {
0 -> null
1 -> "M"
else -> "F"
}
}
}
我想更改微调器的第一个下拉菜单背景颜色。可能吗? 例如,我有 3 个用于性别选择的项目菜单。 1) Select 你的性别 2) 男 3) 女。 我想在微调器下拉列表中显示这 3 个项目。我想将第一项的背景更改为浅蓝色,其他两项应为白色。可以吗
使用微调适配器怎么样?
例如,
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, testarray) {
@Override
public boolean isEnabled(int position) {
return position != 1;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView tv = (TextView) v.findViewById(R.id.spinnerTarget);
tv.setText(testarray.get(position));
switch (position) {
case 0:
tv.setTextColor(Color.RED);
break;
case 1:
tv.setTextColor(Color.BLUE);
break;
default:
tv.setTextColor(Color.BLACK);
break;
}
return v;
}
};
pSpinner.setAdapter(spinnerAdapter);
这是 simple_spinner_dropdown_item.xml 行。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14pt" />
示例代码更改文本颜色。所以你可以尝试改变背景颜色
我为适配器创建了一个单独的 class。在这里
class GenderDropDownAdapter(
private val mContext: Context,
private val genderList: ArrayList<String>
) : ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, genderList) {
override fun areAllItemsEnabled(): Boolean {
return false
}
override fun isEnabled(position: Int): Boolean {
return position != 0
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
var customView = convertView
if (customView == null) {
val mContext = this.context
val vi: LayoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
customView = vi.inflate(R.layout.item_gender_dropdown, null)
}
val genderTextView: TextView = customView?.findViewById<View>(R.id.gender_dropdown_text) as TextView
genderTextView.text = genderList[position]
if (position == 0) {
genderTextView.setBackgroundColor(mContext.resources.getColor(R.color.colorAccent))
genderTextView.setTextColor(mContext.resources.getColor(R.color.white))
} else {
genderTextView.setTextColor(mContext.resources.getColor(R.color.black))
}
return customView
}
}
并在我的 activity
中使用此适配器class PersonalDetailActivity : BaseActivity<AuthenticationViewModel>(), AdapterView.OnItemSelectedListener {
private val genderList = arrayListOf(
"Select Gender",
"Male",
"Female"
)
override fun getLayoutResource() = R.layout.activity_personal_detail
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val genderDropDownAdapter = GenderDropDownAdapter(baseContext, genderList)
gender_spinner.adapter = genderDropDownAdapter
gender_spinner.onItemSelectedListener = this
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
gender = when (position) {
0 -> null
1 -> "M"
else -> "F"
}
}
}