源碼下載地址:http://download.csdn.net/detail/teacodeing/7931787
參考文章:http://blog.csdn.net/guolin_blog/article/details/13171191
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/tabImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/tabTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@color/white"
android:textSize="16sp"/>
</LinearLayout>
/**
* 自定義tab控件
*/
public class TabItemView extends LinearLayout{
private Context context;
private LinearLayout.LayoutParams params = null;
public TabItemView(Context context){
this(context , null);
}
public TabItemView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
}
public void createView(String title , int drawable){
View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
ImageView imageView = (ImageView)view.findViewById(R.id.tabImage);
imageView.setBackgroundResource(drawable);
TextView textView = (TextView)view.findViewById(R.id.tabTitle);
textView.setText(title);
addView(view , params);
}
}
package com.example.myfragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity implements OnClickListener{
private LinearLayout tabsLayout = null;
private FragmentManager fragmentManager = null;
private int size = 4;
private Fragment[] fragments = new Fragment[size];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
initView();
initTabs();
}
public void initView(){
tabsLayout = (LinearLayout)findViewById(R.id.tabs);
}
/**
* 初始化tab值
*/
public void initTabs(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.weight = 1;
for(int i = 0 ; i < size ; i ++){
TabItemView itemView = new TabItemView(this);
if(i == 0)
itemView.createView(getString(R.string.news), R.drawable.tab_news);
else if(i == 1)
itemView.createView(getString(R.string.contacts), R.drawable.tab_contacts);
else if(i == 2)
itemView.createView(getString(R.string.message), R.drawable.tab_message);
else if(i == 3)
itemView.createView(getString(R.string.setting), R.drawable.tab_setting);
itemView.setOnClickListener(this);
itemView.setTag(i);
tabsLayout.addView(itemView , params);
}
setTabSelection(0);
}
/**
* 點擊tab
*/
public void onClick(View v) {
int index = 0;
try{
index = Integer.parseInt(v.getTag().toString());
}catch(Exception e){index = 0;}
if(index >= size)
index = 0;
setTabSelection(index);
}
/**
* 選中tab
* @param index
*/
public void setTabSelection(int index){
clearSelect();
tabsLayout.getChildAt(index).setSelected(true);
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
Fragment fragment = null;
if(fragments[index] != null){
fragment = fragments[index];
transaction.show(fragment);
}else{
if(index == 1){
fragment = new ContactsFragment();
}else if(index == 2){
fragment = new MessageFragment();
}else if(index == 3){
fragment = new SettingFragment();
}else{
fragment = new NewsFragment();
}
fragments[index] = fragment;
transaction.add(R.id.content, fragment);
}
transaction.commit();
}
/**
* 清除tab所有選中狀態
*/
public void clearSelect(){
for(int i = 0 ; i < tabsLayout.getChildCount(); i++){
View v = tabsLayout.getChildAt(i);
v.setSelected(false);
}
}
/**
* 隱藏所有Fragment
* @param transaction
*/
public void hideFragments(FragmentTransaction transaction){
for(Fragment fragment : fragments){
if(fragment != null)
transaction.hide(fragment);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="@drawable/tab_bg" />
</LinearLayout>
fragment其它文章
http://blog.csdn.net/niu_gao/article/details/7163263
http://blog.csdn.net/niu_gao/article/details/7169323
http://blog.csdn.net/niu_gao/article/details/7171697
http://blog.csdn.net/niu_gao/article/details/7172483