Android 在Dialog中使用Spinner时,下三角图标覆盖在按钮上的解决方法

1.如果是系统开发者,则直接修改frameworks/base/core/res/res/drawable/spinner_background_material.xml为如下内容即可:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 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.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
            android:paddingMode="stack"
            android:paddingStart="0dp"
            android:paddingEnd="24dp"
            android:paddingLeft="0dp"
            android:paddingRight="0dp">
    <item
        android:gravity="end|center_vertical"
        android:width="24dp"
        android:height="24dp">
        <!--android:drawable="@drawable/zzz_control_background_24dp_material"-->
        <ripple
            android:color="@color/control_highlight_material"
            android:radius="12dp">
            <item
                android:id="@android:id/mask"
                android:width="24dp"
                android:height="24dp">
                <shape android:shape="oval">
                    <solid android:color="@android:color/white" />
                    <size
                        android:width="24dp"
                        android:height="24dp" />
                </shape>
            </item>
            <item
                android:width="24dp"
                android:height="24dp"
                android:drawable="@drawable/ic_spinner_caret"
                android:gravity="end|center_vertical"/>
        </ripple>
    </item>
    
    <!--<item-->
        <!--android:drawable="@drawable/ic_spinner_caret"-->
        <!--android:gravity="end|center_vertical"-->
        <!--android:width="24dp"-->
        <!--android:height="24dp" />-->
</layer-list>


2、如果是应用开发者,则可以重新给Spinner设置background,方法如下:

1:在res/drawable 下新建 spinner_background.xml 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:opacity="transparent"
    android:paddingEnd="24dp"
    android:paddingLeft="0dp"
    android:paddingMode="stack"
    android:paddingRight="0dp"
    android:paddingStart="0dp">
    <item
        android:width="24dp"
        android:height="24dp"
        android:gravity="end|center_vertical">
        <ripple
            android:color="@color/control_highlight_material"
            android:radius="12dp">
            <item
                android:id="@android:id/mask"
                android:width="24dp"
                android:height="24dp">
                <shape android:shape="oval">
                    <solid android:color="@android:color/white" />
                    <size
                        android:width="24dp"
                        android:height="24dp" />
                </shape>
            </item>
            <item
                android:width="24dp"
                android:height="24dp"
                android:gravity="end|center_vertical">
                <aapt:attr name="android:drawable">
                    <vector xmlns:android="http://schemas.android.com/apk/res/android"
                        android:width="24dp"
                        android:height="24dp"
                        android:tint="?android:attr/colorControlNormal"
                        android:viewportHeight="24.0"
                        android:viewportWidth="24.0">
                        <path
                            android:fillColor="#FF333333"
                            android:pathData="M7,10l5,5,5-5z" />
                    </vector>
                </aapt:attr>
            </item>
        </ripple>
    </item>
</layer-list>


如果 aapt:attr 有问题的话,可以将这部分拆分成一个单独的 ic_spinner_arrow.xml (置于res/drawable中)文件,然后设置item的属性 android:drawable="@drawable/ic_spinner_arrow" ,ic_spinner_arrow.xml 内容如下:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:tint="?android:attr/colorControlNormal">
    <path
        android:pathData="M7,10l5,5,5-5z"
        android:fillColor="#FF333333"/>
</vector>
然后设置

2:在res/color 目录下新建文件 control_highlight_material.xml 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 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:state_enabled="true"
          android:alpha="@dimen/highlight_alpha_material_colored"
          android:color="?android:attr/colorControlActivated" />
    <item android:color="?android:attr/colorControlHighlight" />
</selector>


3:设置Spinner 的background 为 spinner_background

该bug现象如下:




猜你喜欢

转载自blog.csdn.net/songtao542/article/details/77878352
今日推荐