- 论坛徽章:
- 0
|
Android为Spinner添加数据
Android中的Spinner就是下拉框,点击时会出现选择列表,从中可以选择数据。Spinner中的数据必须来自Adapter,因此给它添加数据时需要先定义模板,也就是layout中的xml文件。- < ?xml version="1.0" encoding="utf-8"?>
- < TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvCateItem" android:textSize="14px" android:textColor="@color/black" android:layout_width="wrap_content" android:layout_height="wrap_content">
- < /TextView>
复制代码 我们这里定义其中只是显示,使用的是TextView控件。
然后代码里:- String[] cates=new String[]{
- "未分类","学习"
- };
- ArrayAdapter adapter=new ArrayAdapter(getApplicationContext(),R.layout.spinner_cate_item,cates);
- spCate.setAdapter(adapter);
复制代码 运行,显示效果如下:
|
|