底背景图标动画切换
一。布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" > </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <RelativeLayout android:id="@+id/layout_bottom" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioGroup android:id="@+id/radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/bottombg" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/radio_news" android:layout_width="wrap_content" android:background="@drawable/tab_selector_news" android:button="@null" android:checked="true" /> <RadioButton android:id="@+id/radio_topic" android:layout_width="wrap_content" android:background="@drawable/tab_selector_topic" android:button="@null" /> <RadioButton android:id="@+id/radio_pic" android:layout_width="wrap_content" android:background="@drawable/tab_selector_pic" android:button="@null" /> <RadioButton android:id="@+id/radio_follow" android:layout_width="wrap_content" android:background="@drawable/tab_selector_follow" android:button="@null" /> <RadioButton android:id="@+id/radio_vote" android:layout_width="wrap_content" android:background="@drawable/tab_selector_vote" android:button="@null" /> </RadioGroup> </RelativeLayout> </LinearLayout> </TabHost> </LinearLayout>
二。activity
package com.ct.host; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.RelativeLayout; import android.widget.TabHost; public class MainActivity extends TabActivity { /** Called when the activity is first created. */ TabHost tabHost; TabHost.TabSpec tabSpec; RadioGroup radioGroup; RelativeLayout bottom_layout; ImageView img; int startLeft; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bottom_layout = (RelativeLayout) findViewById(R.id.layout_bottom); tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("news").setIndicator("News").setContent(new Intent(this, TabNewsActivity.class))); tabHost.addTab(tabHost.newTabSpec("topic").setIndicator("Topic").setContent(new Intent(this, TabTopicActivity.class))); tabHost.addTab(tabHost.newTabSpec("picture").setIndicator("Picture").setContent(new Intent(this, TabPicActivity.class))); tabHost.addTab(tabHost.newTabSpec("follow").setIndicator("Follow").setContent(new Intent(this, TabFollowActivity.class))); tabHost.addTab(tabHost.newTabSpec("vote").setIndicator("Vote").setContent(new Intent(this, TabVoteActivity.class))); radioGroup = (RadioGroup) findViewById(R.id.radiogroup); radioGroup.setOnCheckedChangeListener(checkedChangeListener); img = new ImageView(this); img.setImageResource(R.drawable.tab_front_bg); bottom_layout.addView(img); } private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.radio_news: tabHost.setCurrentTabByTag("news"); // moveFrontBg(img, startLeft, 0, 0, 0); moveFrontBg(img, startLeft, 0, 0, 0); startLeft = 0; break; case R.id.radio_topic: tabHost.setCurrentTabByTag("topic"); moveFrontBg(img, startLeft, img.getWidth(), 0, 0); startLeft = img.getWidth(); break; case R.id.radio_pic: tabHost.setCurrentTabByTag("picture"); moveFrontBg(img, startLeft, img.getWidth() * 2, 0, 0); startLeft = img.getWidth() * 2; break; case R.id.radio_follow: tabHost.setCurrentTabByTag("follow"); moveFrontBg(img, startLeft, img.getWidth() * 3, 0, 0); startLeft = img.getWidth() * 3; break; case R.id.radio_vote: tabHost.setCurrentTabByTag("vote"); moveFrontBg(img, startLeft, img.getWidth() * 4, 0, 0); startLeft = img.getWidth() * 4; break; default: break; } } }; private void moveFrontBg(View v, int startX, int toX, int startY, int toY){ TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY); anim.setDuration(200); anim.setFillAfter(true); v.setAnimation(anim); } }
(在F:\java\MyTabHost2)