androidstudio下拉菜单spinner用法
直接上代码
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="">
<!-- 定义下拉菜单-->
<Spinner
android:id = "@+id/Spinner_id"
android:layout_width = "wrap_content"
android:layout_height ="wrap_content" >
</Spinner >
</LinearLayout>
Java
package ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
//android下拉菜单spinner的使用方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
Spinner mSpinner = (Spinner)findViewById(.Spinner_id);
ArrayList<String> list = new ArrayList<String>();
("aaa");
("bbb");
("ccc");
("ddd");
("eee");
("fff");
//为下拉列表定义一个适配器
final ArrayAdapter<String> ad = new ArrayAdapter<String>(this,.simple_spinner_item, list);
//设置下拉菜单样式。
(.simple_spinner_dropdown_item);
//添加数据
(ad);
//点击响应事件
(new (){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
效果: