【发布时间】:2015-05-25 07:31:24
【问题描述】:
我正在为我的应用程序使用 appcompat 主题。需要知道如何向微调器显示下划线。它只是显示锚。我尝试使用 android:background 设置下划线,但它使锚点消失。
【问题讨论】:
标签: android spinner android-appcompat underline
我正在为我的应用程序使用 appcompat 主题。需要知道如何向微调器显示下划线。它只是显示锚。我尝试使用 android:background 设置下划线,但它使锚点消失。
【问题讨论】:
标签: android spinner android-appcompat underline
更新您的支持库并在 XML 中使用
请将此样式添加到您的 Spinner
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
【讨论】:
android:backgroundTint="@color/your_color"
style="@style//Widget.Material.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 可绘制对象:
<?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-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文件夹中的各种line,所以只能有1个)。
视觉差异(来自 android studio xml 预览窗口)如下所示:
第一行是我的自定义下划线微调器,第二行是默认 Base.Widget.AppCompat.Spinner.Underlined
【讨论】:
应用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" />
【讨论】:
在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>
【讨论】: