我正在为我的应用程序使用appcompat主题。需要知道我如何显示下划线到微调器。它只是显示锚。我尝试使用android:background设置下划线但它会使锚点消失。
更新您的支持库和XML使用
请将此样式添加到您的Spinner中
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
这是在appcompat主题中更改微调器和下划线颜色的hacky(而不是完美)方法。主要是我定制Android支持库图像和xml文件来改变颜色。
1)去支持库包并复制2张图片(或从这篇文章的底部下载我的自定义)
/your-app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.0/res/drawable-hdpi/abc_spinner_mtrl_am_alpha.9.png
和
/your-app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.0/res/drawable-hdpi/abc_textfield_default_mtrl_alpha.9.png
2)制作这些图像的副本
3)更改abc_spinner_mtrl_am_alpha.9.png的颜色(警告:保留黑色边框,因为它是9补丁)
4)改变abc_textfield_default_mtrl_alpha.9.png第二个底线的颜色(你可以在下面附上的小图中看到)
5)保存文件并将其移动到项目可绘制文件中
6)创建bottom_line_color.xml drawable:
<?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>
7)创建spinner_bottom_line.xml
<?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>
附:我无法实现与默认微调器相同的视觉样式(下面显示的视觉更改)。如果您开始使用此自定义微调器主题,则应在所有项目中使用它。
所以添加到values / styles.xml
<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文件夹。您可以在我上面显示的相同路径中找到尺寸。流行的尺寸很少:
drawables-mdpi 20x26
drawables-hdpi 29x38
drawables-xhdpi 38x50
drawables-xxhdpi 74x98
您可以从此处获取我的自定义图像:
my_custom_abc_spinner_mtrl_am_alpha:
my_custom_abc_textfield_default_mtrl_alpha:
Spinner的例子是(xxhdpi),line是mdpi(因为我们在各种drawable文件夹中不需要各种行,所以我们只能有1行)。
视觉差异(来自android studio xml预览窗口)如下所示:
第一行是我的自定义下划线微调器,第二行是默认的Base.Widget.AppCompat.Spinner.Underlined
在styles.xml中
<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>
应用style="@style/Base.Widget.AppCompat.Spinner.Underlined"
并没有显示出任何差异。然后将android:backgroundTint
和android:backgroundTintMode
给予旋转器并且它起作用。
<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" />