干货例程"/>
Android Studio 实战干货例程
Android Studio 例程
android studio 3.3.2版本的开发环境
讲解一些基本控件Spinner 下拉列表的控件,
初学阶段的童靴们看过来。
首先创建MainActivity视图直接代码拉过来
// An highlighted blockpublic class SpinnerDropdownActivity extends AppCompatActivity {//声明数组private String[] germsArray = {"病毒", "阮病毒", "细菌", "蓝藻", "放线菌", "真菌","植物"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_spinner_dropdown);//数组适配器ArrayAdapter<String> germsAdapt = new ArrayAdapter<String>(this,R.layout.item_select, germsArray);germsAdapt.setDropDownViewResource(R.layout.item_dropdown);Spinner sp = findViewById(R.id.sp_dropdown);sp.setPrompt("请选择生物");sp.setAdapter(germsAdapt);sp.setSelection(0);sp.setOnItemSelectedListener(new MySelectedListener());}public class MySelectedListener implements AdapterView.OnItemSelectedListener {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(SpinnerDropdownActivity.this,"您选择的是"+germsArray[position],Toast.LENGTH_SHORT).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}}
}
创建两个布局文件
1. item_select.xml
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""android:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:singleLine="true"android:textColor="#0000ff"android:textSize="17sp" />
2.item_dropdown.xml
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""android:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center"android:singleLine="true"android:textColor="#ff0000"android:textSize="17sp" />
这是新手道路上的实战者1
下面加入图标的下拉列表
用到SimpleAdapt类
看代码
// An highlighted block
public class SpinnerIconActivity extends AppCompatActivity {private Spinner mspinner;private String[] arrayname = {"病毒", "阮病毒", "细菌", "蓝藻", "放线菌", "真菌", "植物"};private int[] arrayIcon = {R.mipmap.germs, R.mipmap.rgerms, R.mipmap.xijun, R.mipmap.lanzao,R.mipmap.fangxianjun, R.mipmap.zhengjun, R.mipmap.zhiwu};@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_icon_spinner);mspinner = findViewById(R.id.Icon_spinner_page);List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();for (int i = 0; i < arrayIcon.length; i++) {Map<String, Object> item = new HashMap<String, Object>();item.put("icon", arrayIcon[i]);item.put("name", arrayname[i]);list.add(item);}SimpleAdapter simpleAdapter = new SimpleAdapter(SpinnerIconActivity.this,list, R.layout.item_spinnericon, new String[]{"icon", "name"}, new int[]{R.id.iv_icon, R.id.tv_name});mspinner.setPrompt("请选择生物");mspinner.setSelection(0);mspinner.setAdapter(simpleAdapter);mspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(SpinnerIconActivity.this,"您选择的是"+arrayname[position],Toast.LENGTH_SHORT).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}
}
这是Activity布局文件
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Spinnerandroid:id="@+id/Icon_spinner_page"android:layout_width="match_parent"android:layout_height="wrap_content"android:spinnerMode="dialog"></Spinner></LinearLayout>
这是SimpleAdapt中的适配器布局文件
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv_icon"android:layout_width="120dp"android:layout_height="120dp" /><TextViewandroid:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="120dp"android:layout_marginRight="15dp"android:textSize="30sp"android:textColor="@color/colorAccent"android:gravity="center"android:textAlignment="viewEnd"/>
</LinearLayout>
看效果图
Authors
: Chase
: Zhou
看看上面的代码是不是很简单易懂好实践 ↩︎
更多推荐
Android Studio 实战干货例程
发布评论