美文网首页
Spinner样式修改(1)

Spinner样式修改(1)

作者: 100个大西瓜 | 来源:发表于2021-06-16 19:29 被阅读0次

    20210603
    有个需求如下:


    需求图片

    做了好久都没有满意的效果,试了几种方式,包括
    设置了drawableEnd,如下,都没有效果:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <androidx.appcompat.widget.AppCompatCheckedTextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|start"
            android:drawableEnd="@drawable/spinner_drawable"
            android:duplicateParentState="true"
            android:gravity="center_vertical|start"
            android:textColor="@drawable/spinner_text_color"
            tools:checked="true"
            tools:text="广州市" />
    
    </FrameLayout>
    

    自己搞不出来,上网搜了一些相关的,
    Spinner样式大全_spinner用法详解
    其中一个接近上面的需求,

    参考图片

    sdk-30的android.R.layout.simple_list_item_checked,代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2006 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
      
              http://www.apache.org/licenses/LICENSE-2.0
      
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/textCheckMark"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />
    

    其中很关键的代码是:android:checkMark="?android:attr/textCheckMark",搜了其中textCheckMark的实现

    <item name="textCheckMark">@drawable/indicator_check_mark_dark</item>
    

    顺藤摸瓜,来到@drawable/indicator_check_mark_dark,代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2008 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
      
              http://www.apache.org/licenses/LICENSE-2.0
      
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_checked="true"
            android:drawable="@drawable/btn_check_buttonless_on" />
    
        <item android:state_checked="false"
            android:drawable="@drawable/btn_check_buttonless_off" />
            
        <item
            android:drawable="@drawable/btn_check_buttonless_off" />
    
    </selector>
    

    其中有三个标记,分别是选中状态,未选中状态,默认状态的图片,其中在spinner未点开时的图片是默认状态,
    所以需求马上就可以得到解决了,直接抄袭 android.R.layout.simple_list_item_checked,
    得到我们的目标代码:item_spinner.xml

    <?xml version="1.0" encoding="utf-8"?>
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:checkMark="@drawable/spinner_drawable"
        android:textColor="@drawable/spinner_text_color"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />
    

    修改了这2项, android:checkMark,android:textColor,图片和字体颜色,
    drawable/spinner_drawable.xml也需要配置与indicator_check_mark_dark相同的三种状态。
    如下:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_spinner_checked" android:state_checked="true" />
        <item android:drawable="@drawable/ic_spinner_unchecked" android:state_checked="false" />
        <item android:drawable="@drawable/ic_spinner_unchecked" />
    </selector>
    

    ic_spinner_checked,ic_spinner_unchecked 只是2个普通的xml图片

    drawable/spinner_text_color.xml配置了两种种状态的颜色,如下:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/blue" android:state_checked="true" />
        <item android:color="@android:color/black" />
    </selector>
    

    效果图如下:


    未点击时状态图 点击时的状态图

    基本完成,能满足需求了。这比起去写自定义适配器来的方式简单多了,也优雅。
    还是要多多看源码的实现。

    相关文章

      网友评论

          本文标题:Spinner样式修改(1)

          本文链接:https://www.haomeiwen.com/subject/qpfteltx.html