【问题标题】:Spinner with multiple colors多种颜色的微调器
【发布时间】:2019-12-02 19:58:13
【问题描述】:

我已设法使用以下代码更改微调器文本和背景颜色。

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)

当微调器打开但关闭时,上面创建了一个带有白色文本的黑色背景,我的文本是黑底黑字,因为我的布局背景也是黑色的。如何将微调器关闭状态文本更改为白色?

【问题讨论】:

标签: android kotlin


【解决方案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>

【讨论】:

    【解决方案2】:

    像这样制作你的微调器

     <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
    }
    

    });

    【讨论】:

    • 这是我已经拥有的。我希望将微调器关闭为:背面为白色文本,当微调器打开时,白色为黑色。
    • 您希望您选择的项目文本颜色为黑底白字吗?
    • 是的,在您做出选择之前。当您到达表单时,背景是黑白文本。当您打开微调器时,白底黑字。
    • 我已经更改了答案,请立即检查,它会起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多