Gridview
Gridview布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--
GridView是可滚动的网格。一般用来显示多张图片。
android:horizontalSpacing="5dp" 两列之间的间距是5dp
android:verticalSpacing="5dp" 两行之间的间距是5dp
android:stretchMode="spacingWidth" 缩放与列宽大小同步
android:numColumns="auto_fit" 本来是一行显示几个,现在改为自动分配
-->
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:verticalSpacing="10dp" />
</LinearLayout>
item—gridview布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="@drawable/address_book" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="名字"
android:textColor="#ffffff" />
</LinearLayout>
在fragment等视图控件中依然要使用自定义函数进行空间的初始化
private void addview() {
// TODO Auto-generated method stub
List<Bean> mDatas = new ArrayList<Bean>();
// Bean bean=new Bean("1",R.drawable.address_book,getActivity().getApplicationContext());
// mDatas.add(bean);
Bean bean=new Bean("1",R.drawable.address_book);
mDatas.add(bean);
bean=new Bean("2",R.drawable.address_book);
mDatas.add(bean);
bean=new Bean("3",R.drawable.address_book);
mDatas.add(bean);
bean=new Bean("4",R.drawable.address_book);
mDatas.add(bean);
bean=new Bean("5",R.drawable.address_book);
mDatas.add(bean);
bean=new Bean("6",R.drawable.address_book);
mDatas.add(bean);
mAdapter=new CommonAdapter<Bean>(getActivity().getApplicationContext(), mDatas, R.layout.item_gridview) {
public void convert(ViewHolder helper, Bean item) {
// TODO Auto-generated method stub
helper.setText(R.id.name,item.title());
helper.setImageResource(R.id.imageview1, item.imgid());
}
};
gridview.setAdapter(mAdapter);
Spinner
使用的都是android系统所提供的组件
list.add("北京");
list.add("上海");
list.add("广州");
list.add("深圳");
SpnnerAdapter=new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_spinner_item, list);
SpnnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(SpnnerAdapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
/* 将所选mySpinner 的值带入myTextView 中 */
Toast.makeText(getActivity().getApplicationContext(),"您选择的是:" + SpnnerAdapter.getItem(arg2),Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
自定义的Spinner
即实现有图片的下拉列表
// 第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项
dataList = new ArrayList<Map<String, Object>>();
getData();
myTextView = (TextView) findViewById(R.id.textView);
mySpinner = (Spinner) findViewById(R.id.spinner);
// 第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。
adapter=new SimpleAdapter(this, dataList, R.layout.item, new String[]{"image","text"}, new int[]{R.id.image,R.id.text});
// 第三步:为适配器设置下拉列表下拉时的菜单样式。
adapter.setDropDownViewResource(R.layout.item);
// 第四步:将适配器添加到下拉列表上
mySpinner.setAdapter(adapter);
// 第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中
mySpinner
.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
/* 将所选mySpinner 的值带入myTextView 中 */
myTextView.setText("您选择的是:" + adapter.getItem(arg2));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
myTextView.setText("NONE");
}
});
private void getData() {
// TODO Auto-generated method stub
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.ic_launcher);
map.put("text", "北京");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("image", R.drawable.ic_launcher);
map2.put("text", "上海");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("image", R.drawable.ic_launcher);
map3.put("text", "广州");
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("image", R.drawable.ic_launcher);
map4.put("text", "深圳");
dataList.add(map);
dataList.add(map2);
dataList.add(map3);
dataList.add(map4);
}