一、Spinner自定义下拉菜单
private Spinner mySpinner;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
final String[] countriesStr = { "台北市", "台北县", "台中市", "高雄市" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.myspinner, R.id.textView1, countriesStr);
//默认:ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,allCountries);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//右边有radio,可去掉_dropdown
mySpinner = (Spinner) findViewById(R.id.spinner1);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
toast(countriesStr[arg2]);//toast(arg0.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void toast(String str) {
Toast.makeText(SpinnerNew.this, str, Toast.LENGTH_LONG).show();
}
myspinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/linearLayout2"
android:layout_height="wrap_content"
android:background="@color/yellow">
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"></TextView>
</LinearLayout>
</LinearLayout>
二、动态添加和删除选项
/* 将值新增至adapter */
String newCountry = "hello";
adapter.add(newCountry);
int position = adapter.getPosition(newCountry);
mySpinner.setSelection(position);
/* 将选中项从adapter移除 */
adapter.remove(mySpinner.getSelectedItem().toString());
- 大小: 17.3 KB
- 大小: 26.8 KB
分享到:
相关推荐
Spinner下拉菜单多选,在布局xml文件直接作为控件使用,在activity里面调用就行了
android studio,demo,Spinner下拉菜单多选的实现,android studio3.0.1的环境下使用。
android自定义Spinner下拉菜单(下拉列表框)样式
spinner 下拉菜单源码
android 之Spinner下拉菜单实现级联,需要的朋友可以参考一下
,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样 步骤: 1、定义Spinner控件 代码如下:<Spinner android:id=”@+id/spinner”android:layout_width u201dfill_parent”android:...
修改spinner字体大小及颜色,及点击下拉列侯弹出的列表样式
对于android项目开发来说,常常会用到Spinner实现下拉框的效果。而对于Spinner加载适配器的方法有多种: 1.直接加载android自带的ArrayAdapter,SimpleAdapter; 2.自定义继承...3.Spinner下拉菜单框显示样式;
android 下拉菜单spinner几种使用方法解释
实现二级联动下拉菜单功能,点击选中后对应输出省市名称
主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
自定义下拉菜单模式----Spinner与setDropDownViewResource的应用.
android 自定义下拉菜单模式——Spinner与setDropDownViewResource
spinner+popupwindow 实现折叠菜单 1分大放送了。
Android UI设计 下拉菜单Spinner用法
android 下拉菜单的模板 spinner example 供参考学习
使用的Spinner,自然在Android中也可以实现,而且很简单,下面的代码将教你一步一步的使用Spinner,由于代码在代码中已经有很多注释,故我这边就不多写了,看官看代码就知其意了。教程见:...