android实现下拉框(spinner),⾃⼰定义⼤⼩颜⾊背景位置,去
掉默认样式⿊边
1. 实现最简单的spinner
xml⽂件,有⼀个TextView,⼀个Spinner:
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
.
java⽂件
public class MainActivity extends ActionBarActivity {
private static final String[] name={"刘备","关⽻","张飞","曹操","⼩乔"};
private TextView text ;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
text = (TextView) findViewById(View);
spinner = (Spinner) findViewById(R.id.spinner);
//将可选内容与ArrayAdapter连接起来,simple_spinner_item是android系统⾃带样式
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
//设置下拉列表的风格,simple_spinner_dropdown_item是android系统⾃带的样式,等会⾃⼰定义改动
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter 加⼊到spinner中
spinner.setAdapter(adapter);
//加⼊事件Spinner事件监听
spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
}
//使⽤数组形式操作
class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
android layout布局text.setText("我的名字是:"+name[arg2]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
执⾏效果:
—————————————————————
使⽤xml⽂件作为数据源创建adapter:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="songs">
<item>没有⼈</item>
<item>我的快乐时代</item>
<item>黄⾦时代</item>
<item>习惯失恋</item>
<item>你来⾃哪颗星</item>
</string-array>
</resources>
.java⽂件:
public class SpinnerActivity extends Activity {
private TextView text;
private Spinner spinner;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.spinner);
spinner = (Spinner) findViewById(R.id.spinner);
text = (TextView) findViewById(View);
/
/将可选内容与ArrayAdapter连接起来
adapter = ateFromResource(this, R.array.songs, android.R.layout.simple_spinner_item);          //设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter2 加⼊到spinner中
spinner.setAdapter(adapter);
//加⼊事件Spinner事件监听
spinner.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
}
//使⽤XML形式操作
class SpinnerXMLSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
text.setText("你使⽤什么样的⼿机:"+Item(position));
}
public void onNothingSelected(AdapterView<?
> arg0) {
}
}
}
spinner有三个属性能够记⼀下:
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="-50dp"
android:dropDownHorizontalOffset="20dp"
android:popupBackground="#f0000000"
spinnerMode=dropdown时,为下拉模式
spinnerMode=dialog时,会在界⾯中间弹出
android:popupBackground=”#f0000000”,能够去除spinner的默认⿊边dropDownVerticalOffset和dropDownHorizontalOffset都是改变下拉框位置的
2.⾃⼰定义spinner样式
改变字体颜⾊、⼤⼩和背景:
新建⼀个xml布局⽂件,命名为l:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:textColor="#f77718"
android:gravity="left"
android:textSize="15sp"
android:padding="10dp"
android:singleLine="true"
android:text="New Text"
android:id="@+id/textView32" />
再创建⼀个下拉框样式布局的xml⽂件。命名为l:
<?
xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:padding="10dp"
android:singleLine="true"
android:textSize="15sp"
android:textColor="#f77718"
android:gravity="left"
android:background="#aa33ac"
android:id="@+id/textView3333" />
改动之前.java中的
为:
adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,name);
adapter.setDropDownViewResource(R.layout.dropdown_style);
假设下拉框有⿊边,能够在spinner中加上属性android:popupBackground=”#f0000000”,能够去除spinner的默认⿊边,that’s all~最后的效果图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。