带下划线appcompat的Android微调器

带下划线appcompat的Android微调器,android,spinner,android-appcompat,underline,Android,Spinner,Android Appcompat,Underline,我正在为我的应用程序使用appcompat主题。需要知道如何向微调器显示下划线。它只是在抛锚。我尝试使用android:background设置下划线,但它会使锚消失。更新您的支持库并使用XML 请将此样式添加到微调器 style="@style/Base.Widget.AppCompat.Spinner.Underlined" 这是一种在appcompat主题中更改微调器和下划线颜色的老生常谈(但并不完美)方法。要点是我定制了Android支持库图像和xml文件来改变颜色 1) 转到

我正在为我的应用程序使用appcompat主题。需要知道如何向微调器显示下划线。它只是在抛锚。我尝试使用android:background设置下划线,但它会使锚消失。

更新您的支持库并使用XML

请将此样式添加到微调器

    style="@style/Base.Widget.AppCompat.Spinner.Underlined"
这是一种在appcompat主题中更改微调器和下划线颜色的老生常谈(但并不完美)方法。要点是我定制了Android支持库图像和xml文件来改变颜色

1) 转到support library软件包并复制2个图像(或从本文底部下载我的自定义)

2) 复制这些图像

3) 更改abc_spinner_mtrl_am_alpha.9.png的颜色(警告:保留黑色边框,这适用于9面片)

4) 更改abc_textfield_default_mtrl_alpha.9.png第二条底线的颜色(您可以在下面附加的小图中看到)

5) 将文件保存并移动到项目绘图表中

6) 创建可绘制的底线颜色.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-6dp" android:left="-6dp" android:right="-6dp">
    <shape>
        <stroke android:color="@color/brown" android:width="6dp"/>
    </shape>
</item>
您可以从这里拍摄我的自定义图像:

my_custom_abc_spinner_mtrl_am_alpha:

my_custom_abc_textfield_default_mtrl_alpha:

微调器示例是(xxhdpi),line是mdpi(因为我们不需要各种可绘制文件夹中的各种线条,所以我们只能有1条)

视觉差异(来自android studio xml预览窗口)如下所示:


第一行是我的自定义下划线微调器,第二行是styles.xml中的default Base.Widget.AppCompat.spinner.下划线

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:spinnerStyle">@style/holoSpinner</item>
</style>

 <style name="holoSpinner" parent="Widget.AppCompat.Spinner.Underlined">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/colorPrimary</item>
    </style>

@样式/全息旋转器
16便士
@颜色/原色
========================

在布局中

===============================================

在Drawable中编辑\u text\u bottom\u border.xml文件

应用
style=“@style/Base.Widget.AppCompat.Spinner.下划线”
没有显示任何差异。然后将
android:backgroundTint
android:backgroundTintMode
应用于Spinner,效果良好

 <Spinner
     android:id="@+id/spBookingType"
     android:spinnerMode="dropdown"
     android:layout_marginLeft="16dp"
     android:layout_marginRight="16dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     style="@style/Base.Widget.AppCompat.Spinner.Underlined"
     android:backgroundTint="#ff000000"
     android:backgroundTintMode="src_in" />


如何给下划线上色?有没有办法删除下拉箭头并给它加下划线?如果我设置任何背景,下划线就会消失。有解决上述问题的方法吗?如果我使用此样式,我的箭头将替换为默认箭头。
android:backgroundTint=“@color/your_color”
对于android X:
style=“@style//Widget.Material.Spinner.marlined”
我的朋友,你的方法可能是正确的,但如果android it本身提供了一行代码,你就可以实现这一点,为什么要做这么多艰苦的工作?我没有找到改变微调器下划线颜色的方法。如果我在styles.xml中更改了背景,微调器项将消失。所以这个解决方案可以帮助我改变底线颜色,让微调器项目成为一个有趣的解决方案。我发现在微调器下方放置imageview(带有我的线条)更容易。我已经尝试了您的解决方案,但对我来说,箭头显示在左侧。你知道为什么吗?我的9patch res出现了一个错误。它现在工作正常:)
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
   android:insetLeft="@dimen/abc_control_inset_material"
   android:insetTop="@dimen/abc_control_inset_material"
   android:insetBottom="@dimen/abc_control_inset_material"
   android:insetRight="@dimen/abc_control_inset_material">
<selector>
<item android:state_checked="false" android:state_pressed="false">
    <layer-list>
        <item android:drawable="@drawable/my_custom_abc_textfield_default_mtrl_alpha" />
        <item android:drawable="@drawable/my_custom_abc_spinner_mtrl_am_alpha" />
    </layer-list>
</item>
<item>
    <layer-list>
        <item android:drawable="@drawable/my_custom_abc_textfield_default_mtrl_alpha" />
        <item android:drawable="@drawable/my_custom_abc_spinner_mtrl_am_alpha" />
    </layer-list>
</item>
</selector>
</inset>
<style name="My.Spinner.Style" parent="Base.Widget.AppCompat.Spinner.Underlined">
    <item name="android:background">@drawable/spinner_bottom_line</item>
</style>
      <Spinner
            android:id="@+id/account_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/My.Spinner.Style"
            />
drawables-mdpi   20x26

drawables-hdpi   29x38

drawables-xhdpi  38x50

drawables-xxhdpi 74x98
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:spinnerStyle">@style/holoSpinner</item>
</style>

 <style name="holoSpinner" parent="Widget.AppCompat.Spinner.Underlined">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/colorPrimary</item>
    </style>
 <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp">

                        <Spinner
                            android:id="@+id/spinCountry"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/edit_text_bottom_border"
                            android:paddingBottom="10dp" />
                    </android.support.design.widget.TextInputLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="1dp"
            android:left="-3dp"
            android:right="-3dp"
            android:top="-3dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="1dp"
                    android:color="#535353" />
                <!--android:color="#535353" />-->
            </shape>
        </item>
    </layer-list>
 <Spinner
     android:id="@+id/spBookingType"
     android:spinnerMode="dropdown"
     android:layout_marginLeft="16dp"
     android:layout_marginRight="16dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     style="@style/Base.Widget.AppCompat.Spinner.Underlined"
     android:backgroundTint="#ff000000"
     android:backgroundTintMode="src_in" />