Android学习Tabhost、gallery、listview、imageswitcher
Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。TabHost是标签控件类的核心类,也是标签的集合。
1.tabhost定义
android控件中有封装好的tab控件,直接拖一个到xml文件中。下面的代码都是tab控件自己生成的。
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TabHost 12 android:id="@android:id/tabhost" 13 android:layout_width="fill_parent" 14 android:layout_height="fill_parent" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" > 17 18 <LinearLayout 19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:orientation="vertical" > 22 23 <TabWidget 24 android:id="@android:id/tabs" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" > 27 </TabWidget> 28 29 <FrameLayout 30 android:id="@android:id/tabcontent" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" > 33 34 <LinearLayout 35 android:id="@+id/tab1" 36 android:layout_width="match_parent" 37 android:layout_height="match_parent" 38 android:orientation="vertical" > 39 40 </LinearLayout> 41 42 <LinearLayout 43 android:id="@+id/tab2" 44 android:layout_width="match_parent" 45 android:layout_height="match_parent" 46 android:orientation="vertical" > 47 48 </LinearLayout> 49 50 <LinearLayout 51 android:id="@+id/tab3" 52 android:layout_width="match_parent" 53 android:layout_height="match_parent" 54 android:orientation="vertical" > 55 56 </LinearLayout> 57 </FrameLayout> 58 </LinearLayout> 59 </TabHost> 60 61 </RelativeLayout>
main.xml
在后台,可以通过tabhost.addtab方法添加分页。本例添加了3个标签,并且为其中的两个绑定了不同的activity。
1 TabHost tabhost=this.getTabHost(); 2 LayoutInflater.from(this).inflate(R.layout.activity_main,tabhost.getTabContentView(),true); 3 tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("拨号").setContent(new Intent(this,MyGallery.class))); 4 tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("记录").setContent(new Intent(this,RatingListActivity.class))); 5 tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("联系人").setContent(R.id.tab3));
添加标签控件
2.MyGalleryxml
Gallery是相册控件,它可以水平或者垂直浏览多张图片。imgageswitcher控件可以用来以动画的方式切换图片。本例将imageswitcher和gallery控件相结合。gallery存放多张图片,而imageswitcher来显示图片。
首先来看xml文档的定义。
xml文档定义的很简单,只是包含一个布局控件、一个gallery和imageswitcher。
1 Gallery gallery; 2 ImageSwitcher imgwch; 3 //设置图片资源的id 4 private int[] imgIds={R.drawable.imgs1,R.drawable.imgs2,R.drawable.imgs3,R.drawable.imgs4,R.drawable.imgs5,R.drawable.imgs6,R.drawable.imgs7}; 5 public void onCreate(Bundle savebundle) 6 { 7 super.onCreate(savebundle); 8 setContentView(R.layout.imgswitch); 9 imgwch=(ImageSwitcher)findViewById(R.id.imgswitcher1); 10 imgwch.setFactory(this); 11 //设置imageswitcher的图片动画显示 12 imgwch.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 13 imgwch.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 14 gallery=(Gallery)findViewById(R.id.gallery); 15 //定义相册资源的适配器 16 ImageAdapter adapter=new ImageAdapter(this); 17 gallery.setAdapter(adapter); 18 gallery.setOnItemClickListener(new OnItemClickListener() { 19 20 @Override 21 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 22 long arg3) { 23 // TODO Auto-generated method stub 24 //设置imageswitcher的资源id,这里的数组下标示经过处理的,目的是为了能够循环显示图像 25 imgwch.setBackgroundResource(imgIds[arg2%imgIds.length]); 26 } 27 }); 28 } 29 //定义imageswitcher的显示对象 30 public View makeView() { 31 ImageView imageView = new ImageView(this); 32 imageView.setBackgroundColor(Color.TRANSPARENT); 33 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 34 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 35 return imageView; 36 } 37 public class ImageAdapter extends BaseAdapter 38 { 39 int mgallerybackground; 40 private Context context; 41 public ImageAdapter(Context context) 42 { 43 this.context=context; 44 //设置相册图像的显示风格 45 TypedArray typed=obtainStyledAttributes(R.styleable.Gallery); 46 mgallerybackground=typed.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); 47 typed.recycle(); 48 } 49 @Override 50 public int getCount() { 51 // TODO Auto-generated method stub 52 //可以循环浏览图像 53 return Integer.MAX_VALUE; 54 } 55 56 @Override 57 public Object getItem(int position) { 58 // TODO Auto-generated method stub 59 return null; 60 } 61 62 @Override 63 public long getItemId(int position) { 64 // TODO Auto-generated method stub 65 return 0; 66 } 67 68 @Override 69 public View getView(int position, View convertView, ViewGroup parent) { 70 // TODO Auto-generated method stub 71 //设置图像的显示风格和显示资源 72 ImageView img1=new ImageView(context); 73 74 img1.setScaleType(ImageView.ScaleType.FIT_XY); 75 img1.setLayoutParams(new Gallery.LayoutParams(136,88)); 76 img1.setImageResource(imgIds[position%imgIds.length]); 77 img1.setBackgroundResource(mgallerybackground); 78 return img1; 79 }
ImageSwitcher
这里为相册指定了资源,并设置了显示的风格。也为imageswitcher设置了显示的对象,以及动画的淡入和淡出。
通过以上的代码,我们可以单击“拨号”,即可显示gallery页,同时,imageswitcher可以随着gallery浏览对象的变化而变化。
3.RatingList
本例是实现基于RatingBar和Listview的打分应用。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/lvrating" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" > 11 12 </ListView> 13 </LinearLayout>
listlv
这个xml文档用来定义显示的列表,有listview构成。下面需要定义listview的item项。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:gravity="center_vertical" 6 android:orientation="horizontal" > 7 8 <ImageView 9 android:id="@+id/ivLogo" 10 android:layout_width="60dp" 11 android:layout_height="60dp" 12 android:paddingLeft="5dp" 13 android:src="@drawable/ic_launcher" /> 14 15 <RelativeLayout 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_gravity="right" 19 android:orientation="vertical" 20 android:padding="10dp" > 21 22 <TextView 23 android:id="@+id/tvApplicationName" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:textSize="16dp" /> 27 28 <TextView 29 android:id="@+id/tvAuthor" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_below="@id/tvApplicationName" 33 android:textSize="14dp" /> 34 35 </RelativeLayout> 36 37 <RelativeLayout 38 android:layout_width="fill_parent" 39 android:layout_height="wrap_content" 40 android:gravity="right" 41 android:padding="10dp" 42 android:orientation="vertical" > 43 44 <TextView 45 android:id="@+id/tvRating" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:text="5.0" /> 49 50 <RatingBar 51 android:id="@+id/ratingbar" 52 style="?android:attr/ratingBarStyleSmall" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:layout_below="@id/tvRating" 56 android:numStars="5" /> 57 58 </RelativeLayout> 59 </LinearLayout>
list_item
对于listview的使用方法,大家都应该很清楚的。一般定义复杂的列表显示,都需要通过listview以及item组成。在后台可以通过simpleadapter或者baseadapter来绑定数据。
1 public void onCreate(Bundle savedbundle) 2 { 3 super.onCreate(savedbundle); 4 setContentView(R.layout.listv); 5 ListView listview=(ListView)findViewById(R.id.lvrating); 6 final MyBaseadapter adapter=new MyBaseadapter(this); 7 8 listview.setAdapter(adapter); 9 listview.setOnItemClickListener(new OnItemClickListener() { 10 11 @Override 12 public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, 13 long arg3) { 14 // TODO Auto-generated method stub 15 View myView=getLayoutInflater().inflate(R.layout.rating, null); 16 final RatingBar ratingbar=(RatingBar)myView.findViewById(R.id.ratingBar1); 17 ratingbar.setRating(applicationrating[arg2]); 18 new AlertDialog.Builder(RatingListActivity.this).setTitle(applicationNames[arg2]).setMessage("给城市打分").setView(myView) 19 .setPositiveButton("确定", new OnClickListener() { 20 21 @Override 22 public void onClick(DialogInterface dialog, int which) { 23 // TODO Auto-generated method stub 24 adapter.setRating(arg2, ratingbar.getRating()); 25 } 26 }).setNegativeButton("取消", null).show(); 27 } 28 }); 29 }
oncreate
在oncreate方法中为listview绑定数据,并设置listview的监听事件。MyBaseadapter类继承BaseAdapter类。
1 public class MyBaseadapter extends BaseAdapter 2 { 3 4 private Context context; 5 public MyBaseadapter(Context context) 6 { 7 this.context=context; 8 } 9 @Override 10 public int getCount() { 11 // TODO Auto-generated method stub 12 return resIds.length; 13 } 14 15 @Override 16 public Object getItem(int position) { 17 // TODO Auto-generated method stub 18 return null; 19 } 20 21 @Override 22 public long getItemId(int position) { 23 // TODO Auto-generated method stub 24 return 0; 25 } 26 27 @Override 28 public View getView(int position, View convertView, ViewGroup parent) { 29 // TODO Auto-generated method stub 30 TextView tvapplicationname; 31 if(convertView==null) 32 { 33 //对于这里的使用,真的有太多的方法,这里是为了要或者我们定义的那个item.xml。 34 convertView=LayoutInflater.from(context).inflate(R.layout.ratinglist, null); 35 } 36 tvapplicationname=(TextView)convertView.findViewById(R.id.tvApplicationName); 37 tvapplicationname.setText(applicationNames[position]); 38 ImageView ivlogo=(ImageView)convertView.findViewById(R.id.ivLogo); 39 ivlogo.setImageResource(resIds[position]); 40 TextView tvauthor=(TextView)convertView.findViewById(R.id.tvAuthor); 41 tvauthor.setText(authors[position]); 42 TextView tvrating=(TextView)convertView.findViewById(R.id.tvRating); 43 tvrating.setText(String.valueOf(applicationrating[position])); 44 RatingBar ratingbar=(RatingBar)convertView.findViewById(R.id.ratingbar); 45 ratingbar.setRating(applicationrating[position]); 46 return convertView; 47 } 48 public void setRating(int position,float rating) 49 { 50 applicationrating[position]=rating; 51 notifyDataSetChanged(); 52 } 53 54 }
MyBaseAdapter
BaseAdapter中getView方法使用容易出错,经常会出现获取不到xml文档的情况,主要是Layoutinflater.inflate的使用。这里在获取view对象后,然后为view中的每个控件赋值。最后将这个view返回。这里返回的是listview的每一个item。
上面的单击事件中,选择某一项后,可以对该项进行评分。
程序运行的界面,如下:
本例顺利实现了一系列的功能。下例将会带来arcgis中gp模型的建立以及发布。
转载于:https://www.cnblogs.com/ggz19/p/3875748.html
Android学习Tabhost、gallery、listview、imageswitcher相关推荐
- 【Android 学习】之ListView使用大全
Android 学习之ListView使用大全 ListView是列表组件,是android中常用的组件,列表显示信息由三个部分组成. ListView组件. 适配器,用来将用来显示的数据映射到Lis ...
- android学习--TabHost选项卡组件
TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...
- Android学习笔记--7.listView的使用
前言:ListView这个控件,是挺重要的控件,我们在很多应用中都可以见到它,例如联系人列表,微信的聊天和联系人列表界面等等,所以要下功夫好好学了.在这个控件的学习中也是遇到了不少问题,一个就是,我在 ...
- [Android学习笔记]使用ListView
简单使用ListView 关键在于Adatper Adatper用来连接UI与数据源.Adapter既负责提供数据,又负责创建Item视图. 一般步骤: 1.创建list_item.xml,用来创建L ...
- Android学习笔记之ListView与Item的焦点冲突处理
由于ListView的Item需要焦点,Item里面的子控件(如ImageButton,Button,CheckBox等等)也需要焦点的时候,就会出现焦点冲突问题,导致Item无法获得焦点,无法相应I ...
- 【Android -- 学习笔记】ListView 详解
直接继承自 AbsListView,AbsListView 继承自 AdapterView,AdapterView 又继承自 ViewGroup. Adpater 在 ListView 和数据源之间起 ...
- Android学习笔记:Android基础知识点(不断更新中)
1.Android学习笔记:OkHttp 2.Android学习笔记:更新UI的方法(UI线程和非UI线程) 3.Android学习笔记:Volley 4.Android学习笔记:Handler 5. ...
- Android学习笔记26:图片切换控件ImageSwitcher的使用
在Windows操作系统中,要查看多张图片,可以通过使用"Windows照片查看器"在"上一张"和"下一张"之间切换,进行多张图片的浏览. ...
- Android Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...
- Android学习系列(10)--App列表之拖拽ListView(上)
研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...
最新文章
- [转]解决Eclipse中编辑xml文件的智能提示问题
- java新建常量_【Java】常量 - 每日坚果的个人空间 - OSCHINA - 中文开源技术交流社区...
- 昼猫笔记 从此告别复杂代码--JavaScript
- Python数据结构与算法(第五天)
- 偈颂一首《静心莲》【心静如止水,雷打无波澜。佛前忏一刹,万千罪消融。】
- .net框架读书笔记---基础类型
- cocoapods的安装与使用
- 理论计算机图形渲染技术是否已经到了没有什么可以研究的地步了?
- 从零实现SpringBoot简易读写分离,也不难嘛!
- Office365—Exchange管理4—通讯组和安全组
- 万能的林萧说:我来告诉你,一个草根程序员如何进入BAT。 - 今日头条(www.toutiao.com)...
- vn的可变数据类型_可变与不可变数据类型详解
- [Swift通天遁地]二、表格表单-(15)自定义表单文本框内容的格式
- MySQL5.7数据库-索引优化
- Java中int和byte的互相转换
- MFC 通用对话框之“查找/替换“对话框
- div+css入门知识
- MATLAB 8.1 R2013a license.lic 问题
- 计算机教师师徒结对师傅总结,师徒结对师傅总结
- 大话UWB技术之核心命脉:TDOA与TOF
热门文章
- Asp.net MVC4 下二级联动
- 人生轨迹的改变,首要在于思维方式的改变。--转贴 CSDN.NET公司内部论坛:迈向成功的“脑力操”...
- Linux快速构建apache web服务器
- Redis命令 - sortedSet
- 配置rc.local开机自启动文件的疑问?
- LSI SAS 3008配置操作
- iOS解决融云的 attempt to delete row 0 from section 0 which only contains 0 rows before the update 问题
- 在同一个公司死磕5-10年,到底值不值得?
- 《区块链》都火了两年多了,你还不知道它是什么?
- 互联网公司的黑话,你都经历过吗?