我将Android控件的标签控件的学习知识总结一下和大家共享包括(TabHiost)
在Android开发中,切换信息界面就是需要用到标签控件,Android源生提供了TabHost控件。
目前使用的比较少了,大家都喜欢直接自定义。
1、TatHost控件
Tabhost是标签控件的核心类,也是标签的集合。每一个标签是一个TabHost.TabSpec对象。通过TabHost.addTab方法添加多个标签对象。
看看下面的实例:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/button" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="切换到第3个标签" />
</FrameLayout>
gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="30dp" />
<ImageSwitcher android:id="@+id/imageswitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
rating.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center_vertical">
<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:numStars="5"/>
</LinearLayout>
rating_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center_vertical">
<ImageView android:id="@+id/ivLogo" android:layout_width="60dp"
android:layout_height="60dp" android:src="@drawable/icon"
android:paddingLeft="5dp" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/tvApplicationName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="16dp" />
<TextView android:id="@+id/tvAuthor" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@id/tvApplicationName"
android:textSize="14dp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/tvRating" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="5.0" />
<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:numStars="5"
style="?android:attr/ratingBarStyleSmall" android:layout_below="@id/tvRating" />
</RelativeLayout>
</LinearLayout>
java代码文件:
public class Main extends TabActivity implements OnClickListener
{
@Override
public void onClick(View view)
{
// getTabHost().setCurrentTab(2);
getTabHost().setCurrentTabByTag("tab3");
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
/*
* LayoutInflater.from(this).inflate(R.layout.main,
* tabHost.getTabContentView(), true);
*/
LayoutInflater.from(this).inflate(R.layout.main,
tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("切换标签")
.setContent(R.id.button));
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator("相册",
getResources().getDrawable(R.drawable.icon1))
.setContent(new Intent(this, GalleryActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("评分")
.setContent(new Intent(this, RatingListView.class)));
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
}
RatingListView.java
public class RatingListView extends ListActivity
{
private static String[] applicationNames = new String[]
{ "多功能日历", "eoeMarket客户端", "耐玩的重力消砖块", "白社会", "程序终结者" };
private static String[] authors = new String[]
{ "李宁", "eoemobile", "wawa", "ApkHome", "lucyfa" };
private static int[] resIds = new int[]
{ R.drawable.calendar, R.drawable.eoemarket, R.drawable.brick,
R.drawable.whitesociety, R.drawable.terminater };
private static float[] applicationRating = new float[]
{ (float) 5.0, (float) 5.0, (float) 3.5, (float) 5.0, (float) 4.0 };
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
private RatingAdapter raAdapter;
private class RatingAdapter extends BaseAdapter
{
private Context context;
public RatingAdapter(Context context)
{
this.context = context;
layoutInflater = (LayoutInflater) context
.getSystemService(inflater);
}
@Override
public int getCount()
{
return applicationNames.length;
}
@Override
public Object getItem(int position)
{
return applicationNames[position];
}
@Override
public long getItemId(int position)
{
return position;
}
public void setRating(int position, float rating)
{
applicationRating[position] = rating;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
R.layout.rating_main, null);
ImageView ivLogo = (ImageView) linearLayout
.findViewById(R.id.ivLogo);
TextView tvApplicationName = ((TextView) linearLayout
.findViewById(R.id.tvApplicationName));
TextView tvAuthor = (TextView) linearLayout
.findViewById(R.id.tvAuthor);
TextView tvRating = (TextView) linearLayout
.findViewById(R.id.tvRating);
RatingBar ratingBar = (RatingBar) linearLayout
.findViewById(R.id.ratingbar);
ivLogo.setImageResource(resIds[position]);
tvApplicationName.setText(applicationNames[position]);
tvAuthor.setText(authors[position]);
tvRating.setText(String.valueOf(applicationRating[position]));
ratingBar.setRating(applicationRating[position]);
return linearLayout;
}
}
@Override
protected void onListItemClick(ListView l, View view, final int position,
long id)
{
View myView = getLayoutInflater().inflate(R.layout.rating, null);
final RatingBar ratingBar = (RatingBar) myView
.findViewById(R.id.ratingbar);
ratingBar.setRating(applicationRating[position]);
new AlertDialog.Builder(this).setTitle(applicationNames[position])
.setMessage("给应用程序打分").setIcon(resIds[position])
.setView(myView).setPositiveButton("确定", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
raAdapter.setRating(position, ratingBar.getRating());
}
}).setNegativeButton("取消", null).show();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
List<View> viewList = new ArrayList<View>();
viewList.add(getLayoutInflater().inflate(R.layout.rating_main, null));
raAdapter = new RatingAdapter(this);
setListAdapter(raAdapter);
}
}
GalleryActivity.java
public class GalleryActivity extends Activity implements OnItemSelectedListener,
ViewFactory
{
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private ImageAdapter imageAdapter;
private int[] resIds = new int[]
{ R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,
R.drawable.item5, R.drawable.item6, R.drawable.item7,
R.drawable.item8, R.drawable.item9, R.drawable.item10,
R.drawable.item11, R.drawable.item12, R.drawable.item13,
R.drawable.item14, R.drawable.item15 };
private int count = 3;
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context context)
{
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
public int getCount()
{
return Integer.MAX_VALUE;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
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_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id)
{
imageSwitcher.setImageResource(resIds[position % resIds.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
@Override
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;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(this);
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));
}
}