在小牛奶中找到android.support.v4.app.FragmentTabHost然后再布局中定义
<FrameLayout
android:id="@+id/hh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</FrameLayout>
<android.support.v4.app.FragmentTabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabhost"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
</android.support.v4.app.FragmentTabHost>
主java写
package com.example.fragment_text3_tabhost;
import java.util.ArrayList;
import android.R.color;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
FragmentTabHost tabhost;
ArrayList<Class> classdata=new ArrayList<Class>();//装有加载碎片内容的类
String tabname[]={
"首页","发现","我的","泡泡","联系人"
};
int image[]={
R.drawable.image,R.drawable.image2,
R.drawable.image3,R.drawable.image4,R.drawable.image5
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1,找到FragmentTabHost的控件
tabhost=(FragmentTabHost) findViewById(android.R.id.tabhost);
// 2,配置该控件(找到要替换的布局FrameLayout)
tabhost.setup(MainActivity.this, getSupportFragmentManager(), R.id.hh);
// 添加碎片类(内容)
add();
for(int i=0;i<classdata.size();i++){
// 3,添加table new出来了付了一个id
TabSpec tabSpec = tabhost.newTabSpec(i+"");
// 4,找到自定义布局
View view = View.inflate(MainActivity.this, R.layout.tab_item, null);
// 给tab_item布局里面的ImageView添加图片
ImageView imageview=(ImageView) view.findViewById(R.id.item_ImageView);
imageview.setImageResource(image[i]);
// 添加字
TextView text=(TextView) view.findViewById(R.id.text);
text.setText(tabname[i]);//设置字和字的颜色(选择器)
text.setTextColor((ColorStateList)getResources().getColorStateList(R.drawable.color));
// 5,把自定义布局添加到TabSpec中
tabSpec.setIndicator(view);
// 6.把tabSpec添加到tabhost里面
tabhost.addTab(tabSpec,classdata.get(i), null);
}
//设置没有竖白线
tabhost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
// tabhost.setBackgroundColor(Color.parseColor("#ffffff"));
// tabhost.setCurrentTab(0);//设置切换页面
//// 3,添加table new出来了付了一个id
// TabSpec tabSpec2 = tabhost.newTabSpec("2");
//// 4,找到自定义布局
// View view2 = View.inflate(MainActivity.this, R.layout.tab_item, null);
//// 5,把自定义布局添加到TabSpec中
// tabSpec2.setIndicator(view2);
//// 6.把tabSpec添加到tabhost里面往里面设置内容
// //参数1:是新建的标签 参数2:指定标签内容 参数3:是需要设置的bundle对象(一般没有写null)
// tabhost.addTab(tabSpec2, CopyOfFragment_Hom1.class, null);
//
}
private void add() {
// TODO Auto-generated method stub
classdata.add(CopyOfFragment_Hom1.class);
classdata.add(CopyOfFragment_Hom2.class);
classdata.add(CopyOfFragment_Hom3.class);
classdata.add(CopyOfFragment_Hom4.class);
classdata.add(CopyOfFragment_Hom5.class);
}
}
这是CopyOfFragment_Hom1里面的代码
package com.example.fragment_text3_tabhost;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CopyOfFragment_Hom1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.y1, null);
}
}
其他的就是加载布局和他不一样
写出碎片类
image.png
和布局xml
image.png
效果图
image.png