Android 如何在微调器中设置下拉箭头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20422802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set dropdown arrow in spinner?
提问by june
I tried to set spinner with drop down arrow but i couldn't fix it can anyone help me with this? I have attached the source code.
我试图用下拉箭头设置微调器,但我无法修复它任何人都可以帮助我吗?我附上了源代码。
my class file:
我的类文件:
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
}
});
my xml file:
我的 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>
采纳答案by Aravi
copy and paste this xml instead of your xml
复制并粘贴此 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>
回答by asamoylenko
Basically one needs to create a custom background for a spinner. It should be something like this:
基本上需要为微调器创建自定义背景。它应该是这样的:
spinner_background.xml
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>
Then create a custom style for your spinner, where you specify the above selector as background:
然后为您的微调器创建一个自定义样式,您将上面的选择器指定为背景:
<style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner">
<item name="overlapAnchor">true</item>
<item name="android:background">@drawable/spinner_background</item>
</style>
And finally in your app theme you should override two attributes if you want it to be applied all across your app:
最后,在您的应用主题中,如果您希望在整个应用中应用它,您应该覆盖两个属性:
<item name="spinnerStyle">@style/Widget.App.Spinner</item>
<item name="android:spinnerStyle">@style/Widget.App.Spinner</item>
And that's pretty much it.
仅此而已。
回答by Jonik
As a follow-up to another answer, I was asked how I changed the spinner icon to get something like this:
作为另一个答案的后续行动,有人问我如何更改微调图标以获得如下所示的内容:
One pretty easy way is to use a custom spinner item layout:
一种非常简单的方法是使用自定义微调项布局:
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);
In res/layout/view_spinner_item.xml
, define a TextView with android:drawableRight
pointing to the desired icon (along with any customisations to text size, paddings and so on, if you wish):
在 中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"
/>
(For the opened state, just use android.R.layout.simple_spinner_dropdown_item
or similarly create a customised layout if you want to tweak every aspect of your spinner.)
(对于打开状态,android.R.layout.simple_spinner_dropdown_item
如果您想调整微调器的各个方面,只需使用或类似地创建自定义布局。)
To get the background & colours looking nice, set the Spinner's android:background
and android:popupBackground
as shown in that other question. And if you were wondering about the custom font in the screenshot above, you'll need a custom SpinnerAdapter.
要使背景和颜色看起来不错,请设置 Spinner's android:background
andandroid:popupBackground
如其他问题中所示。如果您想知道上面屏幕截图中的自定义字体,您将需要一个自定义 SpinnerAdapter。
回答by jaymeht
One simple way is to wrap your Spinner + Drop Down Arrow Image inside a Layout. Set the background of Spinner as transparent so that the default arrow icon gets hidden. Something like this:
一种简单的方法是将 Spinner + Drop Down Arrow Image 包裹在 Layout 中。将 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>
Here background.xml is a drawable to produce a box type background.
这里 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>
The above code produces this type of a Spinner and icon.
上面的代码生成这种类型的 Spinner 和图标。
回答by Dibyendu Mitra Roy
Attach a Spinner Style using Java Code:
使用 Java 代码附加 Spinner 样式:
First, you need to a layout file such as below:
首先,您需要一个布局文件,如下所示:
<?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" />
Let us name it spinner_item.xml and place it inside res/layouts folder.
让我们将其命名为 spinner_item.xml 并将其放在 res/layouts 文件夹中。
Next, Create a String ArrayList and put all the Spinner options inside it:
接下来,创建一个 String ArrayList 并将所有 Spinner 选项放入其中:
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");
Finally, create the Spinner object and attach the style layout to it.
最后,创建 Spinner 对象并将样式布局附加到它。
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);
Note the Spinner(getActivity()) in the above line will be changed to Spinner(this) if you are writing this from inside Activity rather than from inside a fragment.
请注意,如果您是从 Activity 内部而不是从片段内部编写它,则上一行中的 Spinner(getActivity()) 将更改为 Spinner(this)。
Thats all!
就这样!
Attach a Spinner Style inside Android Layout File:
在 Android 布局文件中附加 Spinner 样式:
First, create a xml file the defines the style attribute (gradient_spinner.xml)
首先,创建一个定义样式属性的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>
Next, inside the style.xml file specify the style and call the gradient_spinner as background
接下来,在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>
Finally, attach the above style to the Spinner:
最后,将上述样式附加到 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" />
Thats it!
就是这样!
回答by Ujjwal
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="16dp"
android:paddingLeft="10dp"
android:spinnerMode="dropdown" />
回答by Anil Jangir
Copy and paste this xml to show as a Dropdown and change your Dropdown color
复制并粘贴此 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"/>
回答by Nagaraja
Set the Dropdown arrow image to spinner like this :
将下拉箭头图像设置为微调器,如下所示:
<Spinner
android:id="@+id/Exam_Course"
android:layout_width="320dp"
android:background="@drawable/spinner_bg"
android:layout_height="wrap_content"/>
Here android:background="@drawable/spinner_bg" the spinner_bg is the Dropdown arrow image.
这里 android:background="@drawable/spinner_bg" spinner_bg 是下拉箭头图像。
回答by RaviTejaSiddabattuni
dummy.xml (drawable should be of very less size. i have taken 24dp)
dummy.xml(drawable 的尺寸应该非常小。我采用了 24dp)
<?xml version="1.0" encoding="utf-8"?>
<layer-list android:opacity="transparent" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="100dp" android:gravity="right" android:start="300dp">
<bitmap android:src="@drawable/down_button_dummy_dummy" android:gravity="center"/>
</item>
</layer-list>
layout file snippet
布局文件片段
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
app:cardElevation="5dp"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/dummy">
</Spinner>
</android.support.v7.widget.CardView>
回答by Shubham Kamlapuri
From the API level 16 and above, you can use following code to change the drop down icon in spinner. just goto onItemSelected in setonItemSelectedListener and change the drawable of textview selected like this.
从 API 级别 16 及以上,您可以使用以下代码更改微调器中的下拉图标。只需转到 setonItemSelectedListener 中的 onItemSelected 并更改像这样选择的 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) {
}
});
hope it will help somebody.
希望它会帮助某人。