activity_mai.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#eee">
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_bar">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
tab_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:src="@drawable/tab_weixin_pressed">
</ImageView>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="10sp"
android:textColor="#ffffff">
</TextView>
</LinearLayout>
tab选项图片文件
tab_home.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/tab_weixin_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/tab_weixin_pressed" android:state_selected="true"/>
<item android:drawable="@drawable/tab_weixin_pressed" android:state_first="true"/>
<item android:drawable="@drawable/tab_weixin_normal"></item>
</selector>
tab_group.xml, tab_search.xm, tab_setting.xml(同tab_home.xml类似)
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
MainFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
}
MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
View view = View.inflate(getApplicationContext(), R.layout.tab_item, null);
TabSpec tabSpec = tabHost.newTabSpec("首页").setIndicator(view);
ImageView imgView = (ImageView) view.findViewById(R.id.imageview);
imgView.setImageResource(R.drawable.tab_home);
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText("首页");
tabHost.addTab(tabSpec, MainFragment.class, null);
view = View.inflate(getApplicationContext(), R.layout.tab_item, null);
tabSpec = tabHost.newTabSpec("通讯").setIndicator(view);
imgView = (ImageView) view.findViewById(R.id.imageview);
imgView.setImageResource(R.drawable.tab_search);
textView = (TextView) view.findViewById(R.id.textview);
textView.setText("通讯");
tabHost.addTab(tabSpec, MainFragment.class, null);
view = View.inflate(getApplicationContext(), R.layout.tab_item, null);
tabSpec = tabHost.newTabSpec("朋友").setIndicator(view);
imgView = (ImageView) view.findViewById(R.id.imageview);
imgView.setImageResource(R.drawable.tab_group);
textView = (TextView) view.findViewById(R.id.textview);
textView.setText("朋友");
tabHost.addTab(tabSpec, MainFragment.class, null);
view = View.inflate(getApplicationContext(), R.layout.tab_item, null);
tabSpec = tabHost.newTabSpec("设置").setIndicator(view);
imgView = (ImageView) view.findViewById(R.id.imageview);
imgView.setImageResource(R.drawable.tab_setting);
textView = (TextView) view.findViewById(R.id.textview);
textView.setText("设置");
tabHost.addTab(tabSpec, MainFragment.class, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
源码地址:
点击打开链接