【问题标题】:Change spinner background color but keep arrow更改微调器背景颜色但保留箭头
【发布时间】:2014-09-16 11:27:07
【问题描述】:

我已经阅读了几篇关于它的文章,但我找不到我需要的东西。 我想保留灰色箭头,但我想从默认样式中删除水平条并具有白色背景。你知道我该怎么做吗?

这是我现在拥有的(默认微调器样式):

这就是我想要的:

【问题讨论】:

  • 你是什么意思?我创建了一个 arrayadapter 来填充数据,但现在更多的是关于样式,而不是数据。
  • 您需要创建自定义 ArrayAdapter 和自定义布局文件,然后在该适配器类 getView() 方法中扩展 .xml 文件;
  • 我的解决方案是否有效?
  • 创建一个image 并将该图像设置为该微调器的background
  • 添加前景或背景为白色

标签: android spinner android-spinner


【解决方案1】:

我根据@Mansur Khan 的回答做了一些修改。

在这种情况下我们不必添加 ImageView,因为微调器已经有一个三角形箭头。所以检查下面的代码:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:background="#FFFFFF">
        <Spinner
            style="@style/Widget.AppCompat.DropDownItem.Spinner"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:id="@+id/sign_up_country"
            />
    </RelativeLayout>

这是截图

之前:

之后:

【讨论】:

  • android:background="@drawable/bg_spinner" -- 这里需要背景图片吗?
  • 没有这个它看起来像第一张照片。
  • 你有图片因为我在互联网上找不到这样的东西吗?
  • bg_spinner 是白色背景,我只是为它添加了圆角和填充。也粘贴在答案中。
  • 是否可以将此微调器的背景颜色更改为蓝色?如果是,我该如何更改?
【解决方案2】:

作为记录,我找到了一个简单的解决方案:将微调器包装在相对布局中并添加图像:

 <RelativeLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/borderbottom_white"<!-- white background with bottom border -->
     android:layout_marginTop="15dp"  >
        <Spinner
        android:id="@+id/postfield_category"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:background="@null"
        android:minHeight="0dp" />
        <ImageView 
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/arrowspinner" />
    </RelativeLayout>

【讨论】:

  • 我知道这已经很老了,但你能告诉我在哪里可以找到可绘制的箭头旋转器吗?
  • Android SDK 附带的 Spinner 图片可以在 \platforms\\data\res\drawable-xhdpi(hdpi,mdpi etc) 中找到。
  • 不是最好的解决方案,TAD(下)和 CANDLER(上)是对的
【解决方案3】:

一个不需要您为箭头创建自己的可绘制对象的简单解决方案是用RelativeLayout 包裹微调器,并在RelativeLayout 中设置背景颜色,而不是微调器:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f00" >
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

【讨论】:

  • 这是一个非常聪明的解决方案。感谢分享!
【解决方案4】:

使用这个:

yourspinner.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    ((TextView) yourspinner.getSelectedView()).setBackgroundColor(getResources()
            .getColor(R.color.your_color));
}

你的班级应该实现 OnItemSelectedListener。

【讨论】:

  • 您能否详细了解如何实现这一点?有没有办法只使用xml和drawable,因为它只是样式
  • 如果您使用 XML,如果我们设置该 xml 文件的 textview 的背景色,箭头将消失。所以这是最简单的实现方式。正如我在许多应用程序中实现的那样。
  • 是的,但是如果我们只是改变背景颜色,我也会有带箭头的水平条。我只需要箭头。
  • 看到这个答案:stackoverflow.com/a/15380215/2655804,在这个他/她想改变文本颜色。
【解决方案5】:

您好,不要将 Spinner 组件包裹在父布局(如 LinearLayout、RelativeLayout 等)周围,这会增加布局解析,只需在 drawable 文件夹下创建一个名为 spinner_bg.xml 的可绘制对象。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@drawable/icn_dropdown_arw" />
    </item>
</layer-list>

将 spinner_bg 设置为您的微调器的背景,它就像魅力一样工作:

<Spinner  
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:padding="5dp"
     android:background="@drawable/spinner_bg" />

【讨论】:

    【解决方案6】:

    我认为不使用复杂布局的最佳方法是:

    将此 xml 用于您的微调器背景,一切顺利!!!

    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/materialBlueGray600" />
            <corners android:radius="3dp" />
        </shape>
    </item>
    <item android:state_selected="true">
        <shape>
            <solid android:color="@color/materialGray50" />
            <corners android:radius="3dp" />
        </shape>
    </item>
    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="@color/materialGray50" />
                    <corners android:radius="3dp" />
                </shape>
            </item>
    
            <item android:gravity="right">
                <bitmap android:antialias="true"  android:gravity="right" android:src="@drawable/ic_expand_small" />
            </item>
        </layer-list>
    </item>
    

    【讨论】:

      【解决方案7】:

      自定义微调器和任何其他 Android 控件的好方法是使用Android Asset Studio 站点并选择Android Holo Colors Generator。这将创建您可能需要的所有资产,包括“下划线”。它还生成实现更改的 XML 文件。

      从该站点下载 ZIP 文件后,您只需将图像和 XML 文件复制到您的项目中。

      然后您可以编辑所需的图像文件以删除下划线。它们是 9-Patch 文件,称为:

      • apptheme_spinner_default_holo_light.9.png
      • apptheme_spinner_disabled_holo_light.9.png
      • apptheme_spinner_focused_holo_light.9.png
      • apptheme_spinner_pressed_holo_light.9.png

      更完整的流程解释,以及查看一些示例XML文件,请参考我的相关回答:

      【讨论】:

        【解决方案8】:

        这是自定义微调器的代码。请检查并告诉我。我还没有测试过这个。所以请在检查后告诉我它是否解决了您的问题。

        <Spinner                                
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/spinner_background"
            android:gravity="center"
            android:paddingRight="10dp"
            android:spinnerMode="dropdown"
            android:textColor="your text color"
            android:textSize="your text size" />
        

        这里是创建微调器背景的drawable(spinner_background.xml)。

        <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        
            <item>
                <shape android:shape="rectangle" >
                    <solid android:color="border color" />
        
                    <corners android:radius="3dp" />
                </shape>
            </item>
            <item
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp">
                <shape android:shape="rectangle" >
                    <solid android:color="@android:color/white" />
        
                    <corners android:radius="3dp" />
                </shape>
            </item>
        
        </layer-list>
        

        编辑:

        请查看此链接,它为您提供了一个想法。 Customize spinner background

        你可以这样做。

        <RelativeLayout 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:background="spinner background image">
        
                <Spinner       
                android:layout_width="match_parent"
                android:layout_height="match_parent"        
                android:background="@null"/>
        
                <ImageView 
                 android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:src="arrow image" />
        
            </RelativeLayout>
        

        【讨论】:

        • 好的,但是箭头呢?有没有办法用 元素创建右下角的灰色小箭头?
        • 请检查编辑并检查您的问题是否已解决。
        【解决方案9】:

               android:layout_alignParentRight="true"
                android:layout_width="@dimen/spinner_width"
                android:layout_height="wrap_content"
                android:id="@+id/spnLocation"
                android:entries="@array/labelFamily"
                android:layout_centerVertical="true"
        
                android:backgroundTint="@color/color_gray"
        

        这对我有用

        【讨论】:

          【解决方案10】:

          下面的布局将在微调器中创建一个带有所需颜色下拉箭头的背景

            <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight=".6"
                  android:background="@drawable/edit_text_rounded_shape"
                  android:gravity="center|center_vertical">
              <Spinner
                  android:id="@+id/spinerComanyName"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:padding="4dp"
                  android:layout_weight=".6"
                  android:layout_gravity="center"
          
                  android:entries="@array/spinner_item"
                  android:spinnerMode="dropdown"
                  android:theme="@style/Spinner"
          
                  />
             </LinearLayout>
          
             <style name="Spinner">
              <!-- Used for the bottom line when not selected / focused -->
              <item name="colorControlNormal">@color/black</item>
              <item name="colorControlActivated">@color/colorPrimary</item>
              <!-- colorControlActivated & colorControlHighlight use the colorAccent          color by default -->
             </style>
          

          edit_text_rounded_shape 提供背景颜色和圆角

             <?xml version="1.0" encoding="utf-8"?>
          
             <shape xmlns:android="http://schemas.android.com/apk/res/android"
                   android:shape="rectangle" android:padding="10dp">
                   <solid android:color="@color/white"/>
                   <stroke android:color="@color/grey"/>
               <corners
               android:bottomRightRadius="15dp"
               android:bottomLeftRadius="15dp"
               android:topLeftRadius="15dp"
               android:topRightRadius="15dp"/>
            </shape>
          

          【讨论】:

            【解决方案11】:

            在使用微调器、按钮和 EditText 并且还需要插入可绘制对象时,我经常将元素(微调器、EditText 等)包装在 FrameLayout 中。检查一个例子:

            <FrameLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content">
            
                                            <Spinner
                                                android:id="@+id/spinner_component"                                
                                                android:layout_width="match_parent"
                                                android:layout_height="45dp"
                                                android:background="@drawable/your_rectangle_style"
                                                android:textColor="@color/your_text_color" />
            
                                            <ImageView
                                                android:layout_width="11dp"
                                                android:layout_height="9dp"
                                                android:src="@drawable/arrow_spinner"
                                                android:layout_gravity="right|center_vertical"
                                                android:layout_marginRight="7dp" />
                                        </FrameLayout>
            

            另一个重要提示是 FrameLayout 允许您在可绘制对象上设置 OnClick 函数,例如。

            【讨论】:

              猜你喜欢
              • 2015-11-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-10-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多