使用tabHost可以实现点击底部菜单跳转Activity的动作,现在大多数APP都用这种方式。
不继承TabActivity实现对应功能。
tabhost.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" //注意id写法,继承TabActivity的xml文件id为android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dp"/> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"> </TabWidget> </LinearLayout> </TabHost>Java代码
package com.example.test; import android.app.Activity; import android.app.ActivityGroup; import android.app.AlertDialog; import android.app.Dialog; import android.app.TabActivity; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.Toast; public class FaceActivity extends ActivityGroup implements OnTabChangeListener{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.tabhost); Drawable drawable=this.getResources().getDrawable(R.drawable.missed_phone); TabHost tabHost=(TabHost)findViewById(R.id.tabhost); //tabHost.setup(); tabHost.setup(this.getLocalActivityManager()); tabHost.addTab(tabHost.newTabSpec("one").setIndicator(View.inflate(FaceActivity.this, R.layout.tabclass,null)) .setContent(new Intent(FaceActivity.this,MainActivity.class))); tabHost.addTab(tabHost.newTabSpec("two").setIndicator("第二页") .setContent(new Intent(FaceActivity.this,PlayerActivity.class))); tabHost.addTab(tabHost.newTabSpec("three").setIndicator("第三页") .setContent(new Intent(FaceActivity.this,JumpActivity.class))); tabHost.setCurrentTab(0); tabHost.setOnTabChangedListener(this); tabHost.getTabWidget().getChildTabViewAt(0).setOnClickListener(new View.OnClickListener(){//tabHost点击引用自http://blog.csdn.net/fuzhongbin/article/details/50456795 @Override public void onClick(View v){ showMessage(); } }); } public void showMessage(){ Toast.makeText(this, "999999", Toast.LENGTH_SHORT).show(); } @Override public void onTabChanged(String tabId){//切换 View tabView=(View)findViewById(R.id.tabcolor); if(tabId.equals("one")){ tabView.setBackgroundColor(Color.GREEN); } } }注意给tabHost添加点击事件后,不会再响应切换事件。
tabclass.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabcolor" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ee22cd" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/missed_phone"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一页" android:layout_gravity="center"/> </LinearLayout>