Android自定义Spinner下拉菜单(下拉列表框)样式
在Android中Spinner就是下拉菜单,它相当于HTML中的<select>标签。
Android中提供的Spinner Widget下拉菜单已经非常好用了,样式也适用,不过我们还是可以通过定义xml的方式来改变下拉菜单的样式。
在layout目录下新建一个xml文件,名字随便(我这里叫myspinner.xml)。在这个文件里面可以定义下拉菜单的样式
我们这里采用TextView来实现
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="24sp"
android:singleLine="true"
style="?android:attr/spinnerDropDownItemStyle" />
在Activity中我们可以这样调用
private static final String[] countriesStr={"","","",""}
mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesStr);
adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
mySpinner.setAdapter(adapter);
利用自定义的xml我们就可以很灵活的来改变下拉菜单的样式。
另外andorid也提供了两种基本的样式
android.R.layout.simple_spinner_item:TextView的下拉菜单
android.R.layout.simple_spinner_dropdown_item:右边带有radio的下拉菜单
============
方法2:
自定义下拉菜单 -------- Spinner
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class SpinnerDrop extends Activity {
/** Called when the activity is first created. */
private List cityList;
private Spinner sp;
private int pos;
//private Animation animation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cityList= new ArrayList();//初始化一个city的String arraylist
countryList.add("长沙");
countryList.add("北京");
countryList.add("邵阳");
pos=0;//标记选择的位置
sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,cityList);
sp.setAdapter(adapter);//通过setAdapter()来读取ArrayAdapter里的数据
sp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){//////下拉列表的选择事件
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
pos = position;//////////////标记选择的位置
TextView tv = (TextView)findViewById(R.id.xs);
tv.setText("selected:"+cityList.get(position).toString());//显示选择的内容
parent.setVisibility(View.VISIBLE);////设置下拉列表的显示状态
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Button add = (Button)findViewById(R.id.add);//添加一个下拉项
add.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et = (EditText)findViewById(R.id.EditText01);
String newstr = et.getText().toString();
cityList.add(newstr);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerDrop.this,android.R.layout.simple_spinner_item,cityList);
sp.setAdapter(adapter);//添加数据后重新设置ArrayAdapter并加载进去
et.setText("");
Toast.makeText(SpinnerDrop.this, "添加成功", Toast.LENGTH_LONG).show();//浮框提示添加成功
}
});
Button del = (Button)findViewById(R.id.del);//删除下拉项
del.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
cityList.remove(pos);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerDrop.this,android.R.layout.simple_spinner_item,cityList);
sp.setAdapter(adapter);
Toast.makeText(SpinnerDrop.this, "删除成功", Toast.LENGTH_LONG).show();
}
});
}
}