列表控件<ListView>
使用了MVC模式
Adapter对象 控制器C
ListView对象 视图V
数据List或数组 模型M
ListView lvCommonListView = (ListView)findViewById(R.id.lvCommonListView);
ArrayAdapter<String> aaData = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
lvCommonListView.setAdapter(aaData);将适配器加入ListView中
// lvCommonListView.setSelection(6);
lvCommonListView.setOnItemClickListener(this);、注册监听事件单击和选中
lvCommonListView.setOnItemSelectedListener(this);
单选和多选ListView
android:checkmark ="@android:attr/textCheckMark"设置主题属性
setChoiceMode(ListView.CHOICE_MODE_SINGLE);设置单选多选
setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);必须要有
列表项的背景色修改
ListView.setSelector(int resID);传一个图片ID
列表项的增删改操作
主要是对模型V(List或数组)进行操作
然后执行adapter的notifyDataSetChanged,通知Adapter对象数据已经变化,系统将自动调用getView方法返回View来重新显示列表项
以下为BaseAdapter类的getView()方法中
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(inflater);(返回一个系统级服务handle)
LinearLayout linearLayout = null;
linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.text, null);
(填充一个布局文件)
TextView textView = ((TextView) linearLayout
.findViewById(R.id.textview));
BaseAdapter类不像Activity类有getLayoutInflater()方法可以获取LayoutInflater.(布局填充器) 所以使用getSystemService方法
下拉列表控件<Spinner>
使用与ListView控件类似,装载数据时也需要创建一个Adapter对象
SimpleAdapter适配器类可以将任意的XML布局文件作为列表项
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
data是一个元素类型为Map<String, ?>的List对象
resource是指定布局文件名
from是指定布局文件中的控件标签在R.id类的变量取个名字
to是布局文件中的android:id的属性值
可扩展的列表控件<ExpandableListActivity>
ListActivity是一个封装了ListView的Activity
ExpandableListActivity是一个封装了ExpandableListView的Activity
ExpandableListView(分为组列表项和子列表项)
对应的适配器为BaseExpandableListAdapter。
getGroupView 返回组列表项
getChildView 返回子列表项
1. 也需要将适配器绑定到ListView上
setListAdapter(adapter);
2. 单击子列表项会弹出一个菜单
registerForContextMenu(getExpandableListView())
3. ExpandableListActivity继承了OnCreateContextMenuListener接口重写其onCreateContextMenu方法,长按子菜单项弹出上下文菜单
4. 重写Activity的onContextItemSelected方法,
设置菜单项单击事件,选中时弹出一个Toast
滚动控件<ScrollView><HorizontalScrollView><Gallery>
水平滚动<ScrollView>垂直滚动<HorizontalScrollView>
一般都在里面定义一个<LinearLayout>标签来使用
水平滚动和垂直滚动标签嵌套使用可实现水平垂直滚动的视图
Gallery 画廊控件
只能水平显示一行,只能水平滑动
需要创建一个ImageAdapter对象继承于BaseAdapter
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(resIds[position % resIds.length]);
(可以达到循环显示图像的效果)
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
getView方法中的position值与getCount方法的返回值相关。
getCount返回一个Integer.MAX_VALUE再取余数即可
自定义标签属性
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
<attr name="icon" format="reference" />
<attr name="tag" format="string" />
<attr name="scrollX" format="dimension" />
<attr name="background" format="reference|color" />
<attr name="focusable" format="boolean" />
<attr name="defaultScreen" format="integer" />
图像切换控件<ImageSwitcher>
需要继承视图工厂接口ViewFactory,并实现makeView方法来创建一个用于显示图像的ImageView对象。该方法与BaseAdapter的getView方法类似。
public View makeView()
{
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
在onCreate方法中创建一个ImageSwitcher对象设置动画效果。
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
SDK\platforms\android-10\data\res\anim下有系统自带的一些动画效果
网格控件<GridView>
需要设置标签的属性columnWidth宽度,以及numColumns指定列数。(默认只显示一列),GridView的单元格会自动换行显示
标签控件<TabHost>
创建TabHost对象一般使用从TabActivity继承的方法getTabHost()。
TabHost是标签集合,每一个标签是一个TabHost.TabSpec(内部类)
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.main,
tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("切换标签")
.setContent(R.id.button)); };
布局文件R.layout.main.xml可以使用<FrameLayout>或<TabHost>标签
addTab方法可以添加多个TabHost.TabSpec对象。
newTabSpec方法中的参数是标签的ID标识符。
从第一个标签切换到第三个标签可以使用如下两种方法:
getTabHost().setCurrentTab(2);
getTabHost().setCurrentTabByTag("tab3");
惰性装载控件<ViewStub>
和<include>标签类似,可以在一个布局文件中引用另一个布局文件
android:layout="@layout/custom” 引用另一个布局文件
区别:
使用<include>将在程序启动时加载全部的控件
而<ViewStub>并不会马上加载全部的控件,只有调用ViewStub.inflate或ViewStub.setVisibility(View.VISIBLE);才会装载引用的控件
<ViewStub>可以完全取代<include>,但还无法取代<merge>