更改 Android 微调器中所选项目的背景颜色
Change background color of the selected item in Android Spinner
我正在开发一个 android 应用程序,并在我的应用程序的许多地方使用 Spinner。
我想要的是改变微调器选中项的背景颜色,这样可以很容易地识别当前选中的项。
我已经检查过这个 link Setting background color for Spinner Item on selection 但是这样做会更改所选的 textview 背景颜色但不会更改其在下拉列表中的颜色,我想更改所选文本的背景颜色textview 什么时候我会看到下拉列表。
我想更改列表中所选项目的颜色,而不是在微调器上,请参见下图。
我怎样才能做到这一点?拜托,有人可以帮我吗?
非常感谢。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">@color/spinner_background</item>
</style>
在颜色文件夹中定义 Spinner_background 颜色..
尝试在可绘制对象中创建一个选择器,例如,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/colorPrimary" />
<item android:drawable="@android:color/transparent" />
</selector>
并将微调器背景设置为
android:background="@drawable/spinner_selector"
您需要在您的适配器中实现以下方法class:
它将帮助你:
int selectedItem = -1;
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
v = super.getDropDownView(position, null, parent);
// If this is the selected item position
if (position == selectedItem) {
v.setBackgroundColor(Color.BLUE);
}
else {
// for other views
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);
现在在下方微调器中选择的项目
selectedItem = position;
我在互联网上搜索了一个合适的解决方案来执行此操作,而无需在 java 代码中对后台行为进行硬编码。
您可以使用 drawables 实现这一点(设置所选项目的背景颜色)。
您需要做什么 将 dropdownViewResource
设置为自定义布局。该布局应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_item_background"
android:gravity="left"
android:padding="8dp" />
在spinner_item_background.xml
中,您可以为每个项目状态定义背景。例如,如果你想按下时有波纹效果,但选中时有实心效果,你可以试试这个:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Activated state is the selected item -->
<item android:state_activated="true" android:drawable="#00ff00"/>
<!-- Pressed state is the one the user is interacting with -->
<item android:state_pressed="true" android:drawable="#00ff00"/>
<!-- The rest of the items -->
<item android:drawable="#ffffff"/>
</selector>
这里是 XML 的解决方案:
微调器看起来像:
<Spinner
android:id="@+id/settingsSleepingTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/spinner_main_button"
android:popupBackground="@color/colorPrimary"
android:textColor="@android:color/white"
android:textSize="20sp"/>
在创建微调器时将 setDropDownViewResource 设置为自定义布局:
adapter.setDropDownViewResource(R.layout.spinner_item);
而 spinner_item.xml 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner"
android:textColor="#ffffff"
android:textSize="20sp" />
最后我们像这样设置@drawable/spinner:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryLight" android:state_hovered="true" />
<item android:drawable="@color/colorPrimaryLight" android:state_selected="true" />
</selector>
希望我的回答对您有所帮助!
在您的 Activity 中创建一个 int
变量 public static int posOfItemSpinnerSelected
:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
posOfItemSpinnerSelected=position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
并在您的适配器中插入此代码
if(position== YourActivity.posOfItemSpinnerSelected){
textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.item_spinner_selected));
} else {
textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.white));
}
我正在开发一个 android 应用程序,并在我的应用程序的许多地方使用 Spinner。 我想要的是改变微调器选中项的背景颜色,这样可以很容易地识别当前选中的项。
我已经检查过这个 link Setting background color for Spinner Item on selection 但是这样做会更改所选的 textview 背景颜色但不会更改其在下拉列表中的颜色,我想更改所选文本的背景颜色textview 什么时候我会看到下拉列表。
我想更改列表中所选项目的颜色,而不是在微调器上,请参见下图。
非常感谢。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">@color/spinner_background</item>
</style>
在颜色文件夹中定义 Spinner_background 颜色..
尝试在可绘制对象中创建一个选择器,例如,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/colorPrimary" />
<item android:drawable="@android:color/transparent" />
</selector>
并将微调器背景设置为
android:background="@drawable/spinner_selector"
您需要在您的适配器中实现以下方法class:
它将帮助你:
int selectedItem = -1;
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
v = super.getDropDownView(position, null, parent);
// If this is the selected item position
if (position == selectedItem) {
v.setBackgroundColor(Color.BLUE);
}
else {
// for other views
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);
现在在下方微调器中选择的项目
selectedItem = position;
我在互联网上搜索了一个合适的解决方案来执行此操作,而无需在 java 代码中对后台行为进行硬编码。 您可以使用 drawables 实现这一点(设置所选项目的背景颜色)。
您需要做什么 将 dropdownViewResource
设置为自定义布局。该布局应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_item_background"
android:gravity="left"
android:padding="8dp" />
在spinner_item_background.xml
中,您可以为每个项目状态定义背景。例如,如果你想按下时有波纹效果,但选中时有实心效果,你可以试试这个:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Activated state is the selected item -->
<item android:state_activated="true" android:drawable="#00ff00"/>
<!-- Pressed state is the one the user is interacting with -->
<item android:state_pressed="true" android:drawable="#00ff00"/>
<!-- The rest of the items -->
<item android:drawable="#ffffff"/>
</selector>
这里是 XML 的解决方案:
微调器看起来像:
<Spinner
android:id="@+id/settingsSleepingTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/spinner_main_button"
android:popupBackground="@color/colorPrimary"
android:textColor="@android:color/white"
android:textSize="20sp"/>
在创建微调器时将 setDropDownViewResource 设置为自定义布局:
adapter.setDropDownViewResource(R.layout.spinner_item);
而 spinner_item.xml 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner"
android:textColor="#ffffff"
android:textSize="20sp" />
最后我们像这样设置@drawable/spinner:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryLight" android:state_hovered="true" />
<item android:drawable="@color/colorPrimaryLight" android:state_selected="true" />
</selector>
希望我的回答对您有所帮助!
在您的 Activity 中创建一个 int
变量 public static int posOfItemSpinnerSelected
:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
posOfItemSpinnerSelected=position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
并在您的适配器中插入此代码
if(position== YourActivity.posOfItemSpinnerSelected){
textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.item_spinner_selected));
} else {
textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.white));
}