【问题标题】:Android spinner with underline appcompat带有下划线 appcompat 的 Android 微调器
【发布时间】:2015-05-25 07:31:24
【问题描述】:

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

【问题讨论】:

    标签: android spinner android-appcompat underline


    【解决方案1】:

    更新您的支持库并在 XML 中使用

    请将此样式添加到您的 Spinner

        style="@style/Base.Widget.AppCompat.Spinner.Underlined"
    

    【讨论】:

    • 如何给下划线上色?
    • 有什么方法可以去掉下拉箭头并加下划线?如果我设置任何背景,下划线就会消失。
    • 以上有什么解决办法吗?如果我使用这种样式,我的箭头会被默认的替换。
    • android:backgroundTint="@color/your_color"
    • 对于 Android X:style="@style//Widget.Material.Spinner.Underlined"
    【解决方案2】:

    这是在 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

    【讨论】:

    • 我的朋友,你的方法可能是正确的,但如果 android 它自己提供了一条线,你可以使用它来实现这一点,为什么要做这么多艰苦的工作?
    • 我没有找到改变微调器下划线颜色的方法。如果我更改了 styles.xml 中的背景,微调器项就消失了。所以这个解决方案可以帮助我改变底线颜色并留下微调项目
    • 一个有趣的解决方案。我发现在我的微调器下方放置一个图像视图(用我的线)更容易。
    • 我已经尝试过您的解决方案,但对我来说,箭头显示在左侧。你知道为什么吗?
    • 我的 9patch res 出错了。现在工作正常:)
    【解决方案3】:

    应用style="@style/Base.Widget.AppCompat.Spinner.Underlined" 并没有显示任何区别。然后将android:backgroundTintandroid: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" />
    

    【讨论】:

      【解决方案4】:

      在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>
      

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

      Drawable 中的edit_text_bottom_border.xml 文件

      <?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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-18
        • 1970-01-01
        • 2020-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多