展会信息港展会大全

Android学习Tabhost、gallery、listview、imageswitcher
来源:互联网   发布日期:2015-12-04 16:23:52   浏览:1214次  

导读:Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。TabH...

Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。TabHost是标签控件类的核心类,也是标签的集合。

1.tabhost定义

android控件中有封装好的tab控件,直接拖一个到xml文件中。下面的代码都是tab控件自己生成的。

复制代码

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

2xmlns:tools="http://schemas.android.com/tools"

3android:layout_width="fill_parent"

4android:layout_height="fill_parent"

5android:paddingBottom="@dimen/activity_vertical_margin"

6android:paddingLeft="@dimen/activity_horizontal_margin"

7android:paddingRight="@dimen/activity_horizontal_margin"

8android:paddingTop="@dimen/activity_vertical_margin"

9tools:context=".MainActivity" >

10

11<TabHost

12android:id="@android:id/tabhost"

13android:layout_width="fill_parent"

14android:layout_height="fill_parent"

15android:layout_alignParentLeft="true"

16android:layout_alignParentTop="true" >

17

18<LinearLayout

19android:layout_width="match_parent"

20android:layout_height="match_parent"

21android:orientation="vertical" >

22

23<TabWidget

24android:id="@android:id/tabs"

25android:layout_width="match_parent"

26android:layout_height="wrap_content" >

27</TabWidget>

28

29<FrameLayout

30android:id="@android:id/tabcontent"

31android:layout_width="match_parent"

32android:layout_height="match_parent" >

33

34<LinearLayout

35android:id="@+id/tab1"

36android:layout_width="match_parent"

37android:layout_height="match_parent"

38android:orientation="vertical" >

39

40</LinearLayout>

41

42<LinearLayout

43android:id="@+id/tab2"

44android:layout_width="match_parent"

45android:layout_height="match_parent"

46android:orientation="vertical" >

47

48</LinearLayout>

49

50<LinearLayout

51android:id="@+id/tab3"

52android:layout_width="match_parent"

53android:layout_height="match_parent"

54android:orientation="vertical" >

55

56</LinearLayout>

57</FrameLayout>

58</LinearLayout>

59</TabHost>

60

61 </RelativeLayout>

复制代码

在后台,可以通过tabhost.addtab方法添加分页。本例添加了3个标签,并且为其中的两个绑定了不同的activity。

1 TabHost tabhost=this.getTabHost();

2LayoutInflater.from(this).inflate(R.layout.activity_main,tabhost.getTabContentView(),true);

3tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("拨号").setContent(new Intent(this,MyGallery.class)));

4tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("记录").setContent(new Intent(this,RatingListActivity.class)));

5tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("联系人").setContent(R.id.tab3));

2.MyGalleryxml

Gallery是相册控件,它可以水平或者垂直浏览多张图片。imgageswitcher控件可以用来以动画的方式切换图片。本例将imageswitcher和gallery控件相结合。gallery存放多张图片,而imageswitcher来显示图片。

首先来看xml文档的定义。

xml文档定义的很简单,只是包含一个布局控件、一个gallery和imageswitcher。

复制代码

1Gallery gallery;

2ImageSwitcher imgwch;

3//设置图片资源的id

4private int[] imgIds={R.drawable.imgs1,R.drawable.imgs2,R.drawable.imgs3,R.drawable.imgs4,R.drawable.imgs5,R.drawable.imgs6,R.drawable.imgs7};

5public void onCreate(Bundle savebundle)

6{

7super.onCreate(savebundle);

8setContentView(R.layout.imgswitch);

9imgwch=(ImageSwitcher)findViewById(R.id.imgswitcher1);

10imgwch.setFactory(this);

11//设置imageswitcher的图片动画显示

12imgwch.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));

13imgwch.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

14gallery=(Gallery)findViewById(R.id.gallery);

15//定义相册资源的适配器

16ImageAdapter adapter=new ImageAdapter(this);

17gallery.setAdapter(adapter);

18gallery.setOnItemClickListener(new OnItemClickListener() {

19

20@Override

21public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

22long arg3) {

23// TODO Auto-generated method stub

24//设置imageswitcher的资源id,这里的数组下标示经过处理的,目的是为了能够循环显示图像

25imgwch.setBackgroundResource(imgIds[arg2%imgIds.length]);

26}

27});

28}

29//定义imageswitcher的显示对象

30public View makeView() {

31ImageView imageView = new ImageView(this);

32imageView.setBackgroundColor(Color.TRANSPARENT);

33imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

34imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

35return imageView;

36}

37public class ImageAdapter extends BaseAdapter

38{

39int mgallerybackground;

40private Context context;

41public ImageAdapter(Context context)

42{

43this.context=context;

44//设置相册图像的显示风格

45TypedArray typed=obtainStyledAttributes(R.styleable.Gallery);

46mgallerybackground=typed.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);

47typed.recycle();

48}

49@Override

50public int getCount() {

51// TODO Auto-generated method stub

52//可以循环浏览图像

53return Integer.MAX_VALUE;

54}

55

56@Override

57public Object getItem(int position) {

58// TODO Auto-generated method stub

59return null;

60}

61

62@Override

63public long getItemId(int position) {

64// TODO Auto-generated method stub

65return 0;

66}

67

68@Override

69public View getView(int position, View convertView, ViewGroup parent) {

70// TODO Auto-generated method stub

71//设置图像的显示风格和显示资源

72ImageView img1=new ImageView(context);

73

74img1.setScaleType(ImageView.ScaleType.FIT_XY);

75img1.setLayoutParams(new Gallery.LayoutParams(136,88));

76img1.setImageResource(imgIds[position%imgIds.length]);

77img1.setBackgroundResource(mgallerybackground);

78return img1;

79}

复制代码

这里为相册指定了资源,并设置了显示的风格。也为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"

3android:layout_width="fill_parent"

4android:layout_height="fill_parent"

5android:orientation="vertical" >

6

7 <ListView

8android:id="@+id/lvrating"

9android:layout_width="fill_parent"

10android:layout_height="wrap_content" >

11

12 </ListView>

13 </LinearLayout>

复制代码

这个xml文档用来定义显示的列表,有listview构成。下面需要定义listview的item项。

复制代码

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3android:layout_width="fill_parent"

4android:layout_height="wrap_content"

5android:gravity="center_vertical"

6android:orientation="horizontal" >

7

8 <ImageView

9android:id="@+id/ivLogo"

10android:layout_width="60dp"

11android:layout_height="60dp"

12android:paddingLeft="5dp"

13android:src="@drawable/ic_launcher" />

14

15 <RelativeLayout

16android:layout_width="wrap_content"

17android:layout_height="wrap_content"

18android:layout_gravity="right"

19android:orientation="vertical"

20android:padding="10dp" >

21

22 <TextView

23android:id="@+id/tvApplicationName"

24android:layout_width="wrap_content"

25android:layout_height="wrap_content"

26android:textSize="16dp" />

27

28 <TextView

29android:id="@+id/tvAuthor"

30android:layout_width="wrap_content"

31android:layout_height="wrap_content"

32android:layout_below="@id/tvApplicationName"

33android:textSize="14dp" />

34

35 </RelativeLayout>

36

37 <RelativeLayout

38android:layout_width="fill_parent"

39android:layout_height="wrap_content"

40android:gravity="right"

41android:padding="10dp"

42android:orientation="vertical" >

43

44<TextView

45android:id="@+id/tvRating"

46android:layout_width="wrap_content"

47android:layout_height="wrap_content"

48android:text="5.0" />

49

50<RatingBar

51android:id="@+id/ratingbar"

52style="?android:attr/ratingBarStyleSmall"

53android:layout_width="wrap_content"

54android:layout_height="wrap_content"

55android:layout_below="@id/tvRating"

56android:numStars="5" />

57

58 </RelativeLayout>

59 </LinearLayout>

复制代码

对于listview的使用方法,大家都应该很清楚的。一般定义复杂的列表显示,都需要通过listview以及item组成。在后台可以通过simpleadapter或者baseadapter来绑定数据。

复制代码

1 public void onCreate(Bundle savedbundle)

2{

3super.onCreate(savedbundle);

4setContentView(R.layout.listv);

5ListView listview=(ListView)findViewById(R.id.lvrating);

6final MyBaseadapter adapter=new MyBaseadapter(this);

7

8listview.setAdapter(adapter);

9listview.setOnItemClickListener(new OnItemClickListener() {

10

11@Override

12public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,

13long arg3) {

14// TODO Auto-generated method stub

15View myView=getLayoutInflater().inflate(R.layout.rating, null);

16final RatingBar ratingbar=(RatingBar)myView.findViewById(R.id.ratingBar1);

17ratingbar.setRating(applicationrating[arg2]);

18new AlertDialog.Builder(RatingListActivity.this).setTitle(applicationNames[arg2]).setMessage("给城市打分").setView(myView)

19.setPositiveButton("确定", new OnClickListener() {

20

21@Override

22public void onClick(DialogInterface dialog, int which) {

23// TODO Auto-generated method stub

24adapter.setRating(arg2, ratingbar.getRating());

25}

26}).setNegativeButton("取消", null).show();

27}

28});

29}

复制代码

在oncreate方法中为listview绑定数据,并设置listview的监听事件。MyBaseadapter类继承BaseAdapter类。

复制代码

1 public class MyBaseadapter extends BaseAdapter

2{

3

4private Context context;

5public MyBaseadapter(Context context)

6{

7this.context=context;

8}

9@Override

10public int getCount() {

11// TODO Auto-generated method stub

12return resIds.length;

13}

14

15@Override

16public Object getItem(int position) {

17// TODO Auto-generated method stub

18return null;

19}

20

21@Override

22public long getItemId(int position) {

23// TODO Auto-generated method stub

24return 0;

25}

26

27@Override

28public View getView(int position, View convertView, ViewGroup parent) {

29// TODO Auto-generated method stub

30TextView tvapplicationname;

31if(convertView==null)

32{

33 //对于这里的使用,真的有太多的方法,这里是为了要或者我们定义的那个item.xml。

34convertView=LayoutInflater.from(context).inflate(R.layout.ratinglist, null);

35}

36tvapplicationname=(TextView)convertView.findViewById(R.id.tvApplicationName);

37tvapplicationname.setText(applicationNames[position]);

38ImageView ivlogo=(ImageView)convertView.findViewById(R.id.ivLogo);

39ivlogo.setImageResource(resIds[position]);

40TextView tvauthor=(TextView)convertView.findViewById(R.id.tvAuthor);

41tvauthor.setText(authors[position]);

42TextView tvrating=(TextView)convertView.findViewById(R.id.tvRating);

43tvrating.setText(String.valueOf(applicationrating[position]));

44RatingBar ratingbar=(RatingBar)convertView.findViewById(R.id.ratingbar);

45ratingbar.setRating(applicationrating[position]);

46return convertView;

47}

48public void setRating(int position,float rating)

49{

50applicationrating[position]=rating;

51notifyDataSetChanged();

52}

53

54}

复制代码

BaseAdapter中getView方法使用容易出错,经常会出现获取不到xml文档的情况,主要是Layoutinflater.inflate的使用。这里在获取view对象后,然后为view中的每个控件赋值。最后将这个view返回。这里返回的是listview的每一个item。

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港