如何更改微调框的背景devise和颜色为Android?

我正在开发一个应用程序,我需要改变微调框的背景布局以匹配背景颜色。 我研究发现,我需要创build一个9补丁图像。 我已经完成了创build9补丁图像,并在应用程序中使用,但它看起来比正常的微调更大,也看不到在微调下拉button。

我很高兴,如果你们给我一个清晰的教程,从开始创build微调9的补丁图像,并在应用程序中使用它。

纺纱工看起来像

代码为微调

<Spinner android:id="@+id/spnIncredientone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/txtMixtureTitle" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:background="@drawable/spinner_background" android:prompt="@string/selectmixture" /> 

你可以像这样在xml中设置spinners的背景颜色:

 android:background="YOUR_HEX_COLOR_CODE" 

并且如果您使用微调器使用下拉菜单,则可以像下面这样设置其背景颜色:

 android:popupBackground="YOUR_HEX_COLOR_CODE" 

你可以改变背景颜色,并像这样做下拉图标

第一步:在drawable文件夹中为spinner的边框创buildbackground.xml。

 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/transparent" /> <corners android:radius="5dp" /> <stroke android:width="1dp" android:color="@color/darkGray" /> </shape> //edited 

第二步:对于微调器的版面devise,使用这个下拉图标或任何图片drop.pnj 像这样使用这个图像

  <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="3dp" android:layout_weight=".28" android:background="@drawable/spinner_border" android:orientation="horizontal"> <Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center" android:background="@android:color/transparent" android:gravity="center" android:layout_marginLeft="5dp" android:spinnerMode="dropdown" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_gravity="center" android:src="@mipmap/drop" /> </RelativeLayout> 

最后看起来像下面的图像,它是在圆形区域中可点击的每一处,而不需要为imageView编写点击列表。

欲了解更多详情,你可以看到这里

在这里输入图像描述

尽pipe这是一个较旧的post,但是当我在查找同样的问题时遇到它,所以我想我也会加上我的两分钱。 这是我的版本的Spinner的背景与DropDown箭头。 只是完整的背景,不仅是箭头。

这是它的外观 Spinner使用spinner_bg.xml的屏幕截图

适用于微调像…

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

spinner_bg.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <color android:color="@color/InputBg" /> </item> <item android:gravity="center_vertical|right" android:right="8dp"> <layer-list> <item android:width="12dp" android:height="12dp" android:gravity="center" android:bottom="10dp"> <rotate android:fromDegrees="45" android:toDegrees="45"> <shape android:shape="rectangle"> <solid android:color="#666666" /> <stroke android:color="#aaaaaa" android:width="1dp"/> </shape> </rotate> </item> <item android:width="30dp" android:height="10dp" android:bottom="21dp" android:gravity="center"> <shape android:shape="rectangle"> <solid android:color="@color/InputBg"/> </shape> </item> </layer-list> </item> </layer-list> 

@color/InputBg应该被你想要的颜色replace为背景。

首先用所需的颜色填充背景。 然后,一个子图层列表创build一个正方形并将其旋转45度,然后第二个具有背景颜色的矩形覆盖旋转的正方形的顶部,使其看起来像向下的箭头。 (在旋转的矩形中有一个额外的笔画是不是真的需要)

您需要为您的微调项目创build一个新的个性化布局,就像这样,我会命名它:

spinner_item.xml:

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#ff0000" /> 

然后在您的微调声明中,您需要让您的微调器在适配器中使用新的布局:

 ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.layout.spinner_item, YOUR_SPINNER_CONTENT); spinner.setAdapter(adapter); 

要从下拉列表中个性化元素,您需要创build另一个布局,我将其命名为spinner_dropdown_item.xml:

 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerDropDownItemStyle" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textColor="#aa66cc"/> 

然后在适配器上:

 ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.layout.spinner_item, YOUR_SPINNER_CONTENT); adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); spinner.setAdapter(adapter); 

正如Jakob所指出的那样 , android:popupBackground是下拉菜单(Spinner的打开状态)的关键属性,但我不是只使用一种颜色,而是使用一个9-patch drawable来获得最佳效果,如下所示:

在这里输入图像描述
menu_dropdown_panel.9.png

请注意,为您select的背景颜色生成此9补丁图像非常容易,例如使用此在线工具,正如我在此答案中所解释的 !

所以,我的微调XML定义如下所示:

 <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/navigationBlue" android:spinnerMode="dropdown" android:popupBackground="@drawable/menu_dropdown_panel" /> 

结果是:

(对于自定义字体,如上面的截图所示, 也需要一个自定义的SpinnerAdapter 。)

至less在Android 4.0+(API级别14+)上工作。

微调代码:

 <TextView android:id="@+id/spinner" android:gravity="bottom" android:layout_marginTop="16dp" android:background="@drawable/spinner_selector" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:paddingLeft="16dp" android:textSize="16sp" android:text="TextView" /> 

spinner_selector.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/spinner_pressed" android:state_enabled="true" android:state_pressed="false" /> <!-- enable --> <item android:drawable="@drawable/spinner_clicked" android:state_pressed="true" android:state_enabled="true" /> <item android:drawable="@drawable/spinner_default" android:state_enabled="false" /> <!-- disable --> </selector> 

spinner_disable.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="#ddf" /> <padding android:bottom="1dp" /> </shape> </item> <item android:bottom="1dp"> <shape android:shape="rectangle" > <solid android:color="#fff" /> <padding android:left="0dp" android:right="0dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="#fff" /> </shape> </item> <item android:gravity="center_vertical|right" android:right="8dp"> <layer-list> <item android:width="12dp" android:height="12dp" android:bottom="10dp" android:gravity="center"> <rotate android:fromDegrees="45" android:toDegrees="45"> <shape android:shape="rectangle"> <solid android:color="#ddf" /> <stroke android:width="1dp" android:color="#aaaaaa" /> </shape> </rotate> </item> <item android:width="30dp" android:height="10dp" android:bottom="21dp" android:gravity="center"> <shape android:shape="rectangle"> <solid android:color="@android:color/white" /> </shape> </item> </layer-list> </item> </layer-list> 

spinner_focused.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="#00f" /> <padding android:bottom="1dp" /> </shape> </item> <item android:bottom="1dp"> <shape android:shape="rectangle" > <solid android:color="#fff" /> <padding android:left="0dp" android:right="0dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="#fff" /> </shape> </item> <item android:gravity="center_vertical|right" android:right="8dp"> <layer-list> <item android:width="12dp" android:height="12dp" android:bottom="10dp" android:gravity="center"> <rotate android:fromDegrees="45" android:toDegrees="45"> <shape android:shape="rectangle"> <solid android:color="#00f" /> <stroke android:width="1dp" android:color="#aaaaaa" /> </shape> </rotate> </item> <item android:width="30dp" android:height="10dp" android:bottom="21dp" android:gravity="center"> <shape android:shape="rectangle"> <solid android:color="@android:color/white" /> </shape> </item> </layer-list> </item> </layer-list> 

spinner_enable.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="#00f" /> <padding android:bottom="1dp" /> </shape> </item> <item android:bottom="1dp"> <shape android:shape="rectangle" > <solid android:color="#BBDEFB" /> <padding android:left="0dp" android:right="0dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="#BBDEFB" /> </shape> </item> <item android:gravity="center_vertical|right" android:right="8dp"> <layer-list> <item android:width="12dp" android:height="12dp" android:bottom="10dp" android:gravity="center"> <rotate android:fromDegrees="45" android:toDegrees="45"> <shape android:shape="rectangle"> <solid android:color="#00f" /> <stroke android:width="1dp" android:color="#aaaaaa" /> </shape> </rotate> </item> <item android:width="30dp" android:height="10dp" android:bottom="21dp" android:gravity="center"> <shape android:shape="rectangle"> <solid android:color="#BBDEFB" /> </shape> </item> </layer-list> </item> </layer-list> 

它工作正常,没有九个补丁的图片。 api 21+ 在这里输入图像描述

在这里输入图像描述

微调代码

 <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/text.white" android:paddingBottom="13dp" android:background="@drawable/bg_spinner"/> 

bg_spinner.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="@color/colorPrimaryDark"/> <corners android:radius="10dp" /> </shape> </item> <item android:gravity="center_vertical|right" android:right="8dp"> <layer-list> <item android:width="12dp" android:height="12dp" android:gravity="center" android:bottom="10dp"> <rotate android:fromDegrees="45" android:toDegrees="45"> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> <stroke android:color="#ffffff" android:width="1dp"/> </shape> </rotate> </item> <item android:width="20dp" android:height="10dp" android:bottom="21dp" android:gravity="center"> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimaryDark"/> </shape> </item> </layer-list> </item> </layer-list> 

spinner_selector.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/spinner_enabled" android:state_enabled="true" android:state_pressed="false" /> <!-- enable --> <item android:drawable="@drawable/spinner_clicked" android:state_enabled="true" android:state_pressed="true" /> <item android:drawable="@drawable/spinner_disabled" android:state_enabled="false" /> <!-- disable --> </selector> 

spinner_enabled.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="#00f" /> <padding android:bottom="2dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="#fff" /> </shape> </item> <item> <rotate android:fromDegrees="90" android:pivotX="100%" android:pivotY="60%" android:toDegrees="135"> <rotate android:fromDegrees="135" android:pivotX="100%" android:pivotY="60%" android:toDegrees="45"> <shape android:shape="rectangle"> <stroke android:width="10dp" android:color="#00f" /> <solid android:color="#00f" /> </shape> </rotate> </rotate> </item> </layer-list> 

spinner_disabled.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="#ddf" /> <padding android:bottom="2dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="#fff" /> </shape> </item> <item> <rotate android:fromDegrees="90" android:pivotX="100%" android:pivotY="60%" android:toDegrees="135"> <rotate android:fromDegrees="135" android:pivotX="100%" android:pivotY="60%" android:toDegrees="45"> <shape android:shape="rectangle"> <stroke android:width="10dp" android:color="#ddf" /> <solid android:color="#ddf" /> </shape> </rotate> </rotate> </item> </layer-list> 

spinner_focused.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="#00f" /> <padding android:bottom="2dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="#BBDEFB" /> </shape> </item> <item> <rotate android:fromDegrees="90" android:pivotX="100%" android:pivotY="60%" android:toDegrees="135"> <rotate android:fromDegrees="135" android:pivotX="100%" android:pivotY="60%" android:toDegrees="45"> <shape android:shape="rectangle"> <stroke android:width="10dp" android:color="#00f" /> <solid android:color="#00f" /> </shape> </rotate> </rotate> </item> </layer-list> 

它工作正常,没有九个补丁的图片。 api 10+ 在这里输入图像描述

它需要在微调中使用透明背景。

spinner_enable.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="#00000000" /> <padding android:bottom="2dp" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#00000000" /> </shape> </item> <item android:bottom="-2dp" android:left="-3dp" android:right="-3dp" android:top="-3dp"> <shape> <solid android:color="#00000000" /> <stroke android:width="2dp" android:color="#00aedb" /> </shape> </item> <item> <rotate android:fromDegrees="90" android:pivotX="100%" android:pivotY="60%" android:toDegrees="135"> <rotate android:fromDegrees="135" android:pivotX="100%" android:pivotY="60%" android:toDegrees="45"> <shape android:shape="rectangle"> <stroke android:width="10dp" android:color="#00aedb" /> <solid android:color="#00aedb" /> </shape> </rotate> </rotate> </item> </layer-list> 

spinner_disable.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="#00000000" /> <padding android:bottom="2dp" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#00000000" /> </shape> </item> <item android:bottom="-2dp" android:left="-3dp" android:right="-3dp" android:top="-3dp"> <shape> <solid android:color="#00000000" /> <stroke android:width="2dp" android:color="#d9dadc" /> </shape> </item> <item> <rotate android:fromDegrees="90" android:pivotX="100%" android:pivotY="60%" android:toDegrees="135"> <rotate android:fromDegrees="135" android:pivotX="100%" android:pivotY="60%" android:toDegrees="45"> <shape android:shape="rectangle"> <stroke android:width="10dp" android:color="#d9dadc" /> <solid android:color="#d9dadc" /> </shape> </rotate> </rotate> </item> </layer-list> 

spinner_selector.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/spinner_enable" android:state_enabled="true" android:state_pressed="false" /> <!-- enable --> <item android:drawable="@drawable/spinner_disable" android:state_enabled="false" /> <!-- disable --> </selector>