如何在微调器中设置下拉箭头?
我试图用下拉箭头设置微调,但我无法修复它可以帮助我吗? 我附上的源代码。我尝试了下面的链接,但不能解决这个问题http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/
我的课堂文件:
import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Toast; public class topup extends Activity { Spinner spinner,spin1,spin2,spin3; EditText ed,ed1; Button btn; List<String> list, list1,list2,list3; private String[] countries_list={"01","02","03","04","05","06","07","08","09","10","11","12"}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.topup); spinner = (Spinner) findViewById(R.id.spinner1); spin1= (Spinner) findViewById(R.id.spinner2); spin2= (Spinner) findViewById(R.id.spinner3); spin3= (Spinner) findViewById(R.id.spinner4); ed = (EditText) findViewById(R.id.editText3); list = new ArrayList<String>(); list.add(""); list.add("select"); list.add("01"); list.add("02"); list.add("03"); list.add("04"); list.add("05"); list.add("06"); list.add("07"); list.add("08"); list.add("09"); list.add("10"); list.add("11"); list.add("12"); ArrayAdapter<String> adp = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list); //adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adp); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { //@Override public void onItemSelected(AdapterView<?> parent, View arg1,int arg2, long arg3) { // TODO Auto-generated method stub String item = spinner.getItemAtPosition(1).toString(); //Toast.makeText(spinner.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); String ss=spinner.getSelectedItem().toString(); String } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } });
我的xml文件:
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/back1" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="20dp" android:background="@drawable/red"> <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="match_parent" android:dropDownWidth="fill_parent" /> </LinearLayout> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignLeft="@+id/linearLayout1" android:layout_alignRight="@+id/linearLayout1" android:layout_below="@+id/linearLayout1" android:layout_marginTop="25dp" android:background="@drawable/red" android:ems="10" android:hint="enter card number" > <requestFocus /> </EditText> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignLeft="@+id/editText1" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="33dp" android:orientation="horizontal" android:background="@drawable/red"> <Spinner android:id="@+id/spinner3" android:layout_width="72dp" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner2" android:layout_width="72dp" android:layout_height="wrap_content" /> <EditText android:id="@+id/editText2" android:layout_width="22dp" android:layout_height="match_parent" android:layout_weight="0.18" android:ems="10" android:hint="enter cvv" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/linearLayout2" android:layout_below="@+id/linearLayout2" android:layout_marginTop="26dp" android:orientation="vertical" android:background="@drawable/red" > </LinearLayout> <Spinner android:id="@+id/spinner4" android:layout_width="15dp" android:layout_height="18dp" android:layout_alignBottom="@+id/linearLayout3" android:layout_alignLeft="@+id/linearLayout3" android:layout_alignRight="@+id/linearLayout3" android:layout_alignTop="@+id/linearLayout3" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/linearLayout3" android:layout_marginTop="18dp" android:text="Add Amount" android:background="@drawable/buttonsty"/> </RelativeLayout>
复制并粘贴这个XML而不是你的XML
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/back1" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="20dp" android:background="@drawable/red"> <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="match_parent" android:dropDownWidth="fill_parent" android:background="@android:drawable/btn_dropdown" /> </LinearLayout> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignLeft="@+id/linearLayout1" android:layout_alignRight="@+id/linearLayout1" android:layout_below="@+id/linearLayout1" android:layout_marginTop="25dp" android:background="@drawable/red" android:ems="10" android:hint="enter card number" > <requestFocus /> </EditText> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignLeft="@+id/editText1" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="33dp" android:orientation="horizontal" android:background="@drawable/red"> <Spinner android:id="@+id/spinner3" android:layout_width="72dp" android:layout_height="wrap_content" android:background="@android:drawable/btn_dropdown" /> <Spinner android:id="@+id/spinner2" android:layout_width="72dp" android:layout_height="wrap_content" android:background="@android:drawable/btn_dropdown" /> <EditText android:id="@+id/editText2" android:layout_width="22dp" android:layout_height="match_parent" android:layout_weight="0.18" android:ems="10" android:hint="enter cvv" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/linearLayout2" android:layout_below="@+id/linearLayout2" android:layout_marginTop="26dp" android:orientation="vertical" android:background="@drawable/red" > </LinearLayout> <Spinner android:id="@+id/spinner4" android:layout_width="15dp" android:layout_height="18dp" android:layout_alignBottom="@+id/linearLayout3" android:layout_alignLeft="@+id/linearLayout3" android:layout_alignRight="@+id/linearLayout3" android:layout_alignTop="@+id/linearLayout3" android:background="@android:drawable/btn_dropdown" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/linearLayout3" android:layout_marginTop="18dp" android:text="Add Amount" android:background="@drawable/buttonsty"/> </RelativeLayout>
作为另一个答案的后续,我被问到如何改变微调图标来得到这样的东西:
一个非常简单的方法是使用自定义微调器项目布局 :
Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.view_spinner_item, ITEMS ); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter);
在res/layout/view_spinner_item.xml
,定义一个带有android:drawableRight
的TextView,指向所需的图标(如果需要,还可以指定文本大小,填充等等):
<?xml version="1.0" encoding="utf-8"?> <!-- Custom spinner item layout --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerItemStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textSize="@dimen/text_size_medium" android:drawablePadding="@dimen/spacing_medium" android:drawableRight="@drawable/ic_arrow_down" />
(对于打开的状态,只要使用android.R.layout.simple_spinner_dropdown_item
或类似的创build一个自定义的布局,如果你想调整微调的每一个方面。)
为了使背景和颜色看起来不错,设置微调的android:background
和android:popupBackground
如在其他问题中所示 。 如果您想知道上面屏幕截图中的自定义字体,则需要自定义SpinnerAdapter 。
基本上需要为微调创build一个自定义的背景。 它应该是这样的:
spinner_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <layer-list> <item> <color android:color="@android:color/white"/> </item> <item> <bitmap android:gravity="center_vertical|right" android:src="@drawable/ic_arrow_drop_down_black_24dp"/> </item> </layer-list> </item> </selector>
然后为您的微调器创build一个自定义样式,在其中指定上述select器作为背景:
<style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner"> <item name="overlapAnchor">true</item> <item name="android:background">@drawable/spinner_background</item> </style>
最后,在你的应用程序主题中,如果你希望在你的应用程序中应用所有的属性,你应该重写两个属性:
<item name="spinnerStyle">@style/Widget.App.Spinner</item> <item name="android:spinnerStyle">@style/Widget.App.Spinner</item>
而这是非常多的。
一个简单的方法是将“微调器+下拉箭头”图像包裹在布局中。 将Spinner的背景设置为透明,以便隐藏默认的箭头图标。 像这样的东西:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/background" android:orientation="horizontal"> <Spinner android:id="@+id/spinner" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="4" android:gravity="center" android:background="@android:color/transparent" android:spinnerMode="dropdown" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:onClick="showDropDown" android:src="@drawable/ic_chevron_down_blue" /> </LinearLayout>
这里的background.xml是一个可绘制的产生箱型背景。
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/transparent" /> <corners android:radius="2dp" /> <stroke android:width="1dp" android:color="#BDBDBD" /> </shape>
上面的代码产生这种types的微调和图标。
使用Java代码附加Spinner样式:
首先,您需要一个布局文件,如下所示:
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerDropDownItemStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="none" android:minHeight="?android:attr/listPreferredItemHeight" />
让我们将其命名为spinner_item.xml,并将其放在res / layouts文件夹中。
接下来,创build一个stringArrayList,并将其中的所有微调选项:
ArrayList<String> spinnerArray = new ArrayList<String>(); spinnerArray.add("Item No. 1"); spinnerArray.add("Item No. 2"); spinnerArray.add("Item No. 3"); spinnerArray.add("Item No. 4");
最后,创buildSpinner对象并将样式布局附加到它。
Spinner spinner = new Spinner(getActivity()); spinner.setTag("some_id"); ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray); spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item); spinner.setAdapter(spinnerArrayAdapter);
请注意,如果您是从Activity内部而不是从片段内写入,则上面一行中的Spinner(getActivity())将更改为Spinner(this)。
就这样!
在Android布局文件中添加一个微调样式:
首先创build一个xml文件,定义样式属性(gradient_spinner.xml)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item><layer-list> <item><shape> <gradient android:angle="90" android:type="linear" /> <stroke android:width="1dp" android:color="@color/colorBackground" /> <corners android:radius="2dp" /> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" /> </shape></item> <item android:right="5dp"> <bitmap android:gravity="center_horizontal|right" android:src="@drawable/expand_icon"> <padding android:right="2dp" /> </bitmap> </item> </layer-list></item> </selector>
接下来,在style.xml文件中指定样式并将gradient_spinner作为背景调用
<style name="spinner_style"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:background">@drawable/gradient_spinner</item> <item name="android:layout_margin">1dp</item> <item name="android:paddingLeft">5dp</item> <item name="android:paddingRight">5dp</item> <item name="android:paddingTop">5dp</item> <item name="android:paddingBottom">5dp</item> </style>
最后,将上面的样式附加到Spinner:
<Spinner android:id="@+id/agent_id_spinner" android:layout_width="match_parent" android:layout_height="40dp" android:textSize="@dimen/title_text_view" style="@style/spinner_style" />
而已!
设置下拉箭头图像像这样微调:
<Spinner android:id="@+id/Exam_Course" android:layout_width="320dp" android:background="@drawable/spinner_bg" android:layout_height="wrap_content"/>
这里的android:background =“@ drawable / spinner_bg”spinner_bg是下拉箭头图片。
<Spinner android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="16dp" android:paddingLeft="10dp" android:spinnerMode="dropdown" />
复制并粘贴此xml以显示为下拉菜单并更改您的下拉颜色
<?xml version="1.0" encoding="UTF-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/back1" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="20dp" android:background="@drawable/red"> <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="match_parent" android:dropDownWidth="fill_parent" android:popupBackground="@drawable/textbox" android:spinnerMode="dropdown" android:background="@drawable/drop_down_large" /> </LinearLayout> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignLeft="@+id/linearLayout1" android:layout_alignRight="@+id/linearLayout1" android:layout_below="@+id/linearLayout1" android:layout_marginTop="25dp" android:background="@drawable/red" android:ems="10" android:hint="enter card number" > <requestFocus /> </EditText> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignLeft="@+id/editText1" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="33dp" android:orientation="horizontal" android:background="@drawable/red"> <Spinner android:id="@+id/spinner3" android:layout_width="72dp" android:layout_height="wrap_content" android:popupBackground="@drawable/textbox" android:spinnerMode="dropdown" android:background="@drawable/drop_down_large" /> <Spinner android:id="@+id/spinner2" android:layout_width="72dp" android:layout_height="wrap_content" android:popupBackground="@drawable/textbox" android:spinnerMode="dropdown" android:background="@drawable/drop_down_large" /> <EditText android:id="@+id/editText2" android:layout_width="22dp" android:layout_height="match_parent" android:layout_weight="0.18" android:ems="10" android:hint="enter cvv" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/linearLayout2" android:layout_below="@+id/linearLayout2" android:layout_marginTop="26dp" android:orientation="vertical" android:background="@drawable/red" > </LinearLayout> <Spinner android:id="@+id/spinner4" android:layout_width="15dp" android:layout_height="18dp" android:layout_alignBottom="@+id/linearLayout3" android:layout_alignLeft="@+id/linearLayout3" android:layout_alignRight="@+id/linearLayout3" android:layout_alignTop="@+id/linearLayout3" android:popupBackground="@drawable/textbox" android:spinnerMode="dropdown" android:background="@drawable/drop_down_large"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/linearLayout3" android:layout_marginTop="18dp" android:text="Add Amount" android:background="@drawable/buttonsty"/>
从API级别16及以上,您可以使用以下代码更改微调器中的下拉图标。 只需转到setonItemSelectedListener中的ItemSelected并更改所选的textview的drawable即可。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // give the color which ever you want to give to spinner item in this line of code //API Level 16 and above only. ((TextView)parent.getChildAt(position)).setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,ContextCompat.getDrawable(Activity.this,R.drawable.icon),null); //Basically itis changing the drawable of textview, we have change the textview left drawable. } @Override public void onNothingSelected(AdapterView<?> parent) { } });
希望它能帮助别人。