我设法使用以下代码更改了微调框的文本和背景颜色。
spinner_layout.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"
android:background="@color/colorWhite"
android:fontFamily="@font/raleway"/>
调用上面的kt文件
ArrayAdapter(requireContext(), R.layout.spinner_layout, companyList)
当微调器打开时,上面创建了一个带有白色文本的黑色背景,但在关闭时,我的文本为黑色,而布局背景也为黑色。如何将微调器关闭状态文本更改为白色?
答案 0 :(得分:1)
使用此功能,您可以在打开和关闭时使用不同的布局。
adapter.setDropDownViewResource(R.layout.your_layout_resource_xml)
当然,在您必须创建自定义适配器之前,但是我想您已经做到了。
要获得有关背景的最佳结果,需要进行更多更改:
how can i change spinner background color?
基本上,您为带有两个不同布局的微调框和微调框项目创建自定义布局
<style name="AppTheme.spinnerStyle"
parent="@android:style/Widget.Material.Light.Spinner">
<item name="android:textColor">@android:color/white</item>
<item name="android:background">@color/colorPrimary</item>
</style>
<style name="AppTheme.spinnerDropDownItemStyle"
parent="@android:style/Widget.Material.DropDownItem.Spinner">
<item name="android:textColor">@android:color/white</item>
<item name="android:background">@color/colorPrimary</item>
</style>
答案 1 :(得分:0)
像这样使您的微调器
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:background="@color/black"
android:minHeight="45dp"
android:padding="3dp">
<Spinner
android:id="@+id/girth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
您的微调器项目视图布局如下
<?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:padding="5dp"
android:textColor="@color/white"
android:textSize="18sp"
android:fontFamily="@font/raleway"/>
然后在您的onItemeSelectedListener中更改选定的文本颜色,例如
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
((TextView)view).setTextColor(Color.WHITE);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});