private void initFragmentTabHost() {
//枚举方式创建
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
//去掉分割线
if (Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
}
for (int i = 0; i < EMAINTABINFO.values().length; i++) {
EMAINTABINFO[] values = EMAINTABINFO.values();
EMAINTABINFO value = values[i];
String title = value.title;
int topResId = value.topResId;
Class clz = value.clz;
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(title);
View indicator = View.inflate(this, R.layout.view_indicator1, null);
TextView tabTitle = indicator.findViewById(R.id.tab_title);
tabTitle.setText(title);
tabTitle.setCompoundDrawablesWithIntrinsicBounds(0, topResId, 0, 0);
tabSpec.setIndicator(indicator);
Bundle bundle = new Bundle();
bundle.putString("args", "" + i);
mFragmentTabHost.addTab(tabSpec, clz, bundle);
if (i == 2) {
indicator.setVisibility(View.INVISIBLE);
}
}
}
public enum EMAINTABINFO {
NEW("综合", R.drawable.tab_icon_new, NewsTabFragment.class),
TWEET("动弹", R.drawable.tab_icon_tweet, TweetTabFragment.class),
QUICKOPTION("", R.drawable.tab_icon_explore, TweetTabFragment.class),
EXPLORE("发现", R.drawable.tab_icon_explore, ExploreTabFragment.class),
ME("我", R.drawable.tab_icon_me, MeTabFragment.class);
public String title;
public int topResId;
public Class clz;
EMAINTABINFO(String title, int topResId, Class clz) {
this.title = title;
this.topResId = topResId;
this.clz = clz;
}
}
//
private void 集合方式创建FragmentTabHost() {
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
//去掉分割线
if (Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
}
List<MainTabInfo> mainTabInfos = new ArrayList<>();
mainTabInfos.add(new MainTabInfo("综合", R.drawable.tab_icon_new, NewsPagerFragment.class));
mainTabInfos.add(new MainTabInfo("动弹", R.drawable.tab_icon_tweet, TweetPagerFragment.class));
mainTabInfos.add(new MainTabInfo("", R.drawable.tab_icon_explore, TweetPagerFragment.class));
mainTabInfos.add(new MainTabInfo("发现", R.drawable.tab_icon_explore, TweetPagerFragment.class));
mainTabInfos.add(new MainTabInfo("我", R.drawable.tab_icon_me, TweetPagerFragment.class));
for (int i = 0; i < mainTabInfos.size(); i++) {
String title = mainTabInfos.get(i).title;
int topResId = mainTabInfos.get(i).topResId;
Class clz = mainTabInfos.get(i).clz;
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(title);
View indicator = View.inflate(this, R.layout.view_indicator1, null);
TextView tabTitle = indicator.findViewById(R.id.tab_title);
tabTitle.setText(title);
tabTitle.setCompoundDrawablesWithIntrinsicBounds(0, topResId, 0, 0);
tabSpec.setIndicator(indicator);
Bundle bundle = new Bundle();
bundle.putString("args", "" + i);
mFragmentTabHost.addTab(tabSpec, clz, bundle);
if (i == 2) {
indicator.setVisibility(View.INVISIBLE);
}
}
}
class MainTabInfo {
public String title;
public int topResId;
public Class clz;
public MainTabInfo(String title, int topResId, Class clz) {
this.title = title;
this.topResId = topResId;
this.clz = clz;
}
}
//
private void 初始化数组方式FragmentTabHost() {
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
//去掉分割线
if (Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
}
String mainTileArr[] = new String[]{"综合", "动弹", "", "发现", "我"};
int topResIdArr[] = new int[]{
R.drawable.tab_icon_new,
R.drawable.tab_icon_tweet,
R.drawable.tab_icon_explore,
R.drawable.tab_icon_explore,
R.drawable.tab_icon_me,
};
for (int i = 0; i < mainTileArr.length; i++) {
String title = mainTileArr[i];
int topResId = topResIdArr[i];
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(title);
View indicatorView = View.inflate(this, R.layout.view_indicator1, null);
TextView tabTitle = indicatorView.findViewById(R.id.tab_title);
tabTitle.setCompoundDrawablesWithIntrinsicBounds(0, topResId, 0, 0);
//设置tabSpec的样式
tabSpec.setIndicator(indicatorView);
Bundle bundle = new Bundle();
bundle.putString("args", "" + i);
if (i == 2) {
indicatorView.setVisibility(View.INVISIBLE);
}
mFragmentTabHost.addTab(tabSpec, NewsPagerFragment.class, bundle);
}
}
//
private void 方式一初始化FragmentTabHost() {
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
View IndicatorView1 = View.inflate(this, R.layout.view_indicator1, null);
TextView tab_title1 = IndicatorView1.findViewById(R.id.tab_title);
tab_title1.setText("综合");
tab_title1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tab_icon_new, 0, 0);
TabHost.TabSpec tab1 = mFragmentTabHost.newTabSpec("tag1");//添加tab标签
tab1.setIndicator(IndicatorView1);//设置样式
Bundle bundle = new Bundle();
bundle.putString("args", "one");
mFragmentTabHost.addTab(tab1, NewsPagerFragment.class, bundle);
View IndicatorView2 = View.inflate(this, R.layout.view_indicator1, null);
TextView tab_title2 = IndicatorView1.findViewById(R.id.tab_title);
tab_title2.setText("动弹");
tab_title2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tab_icon_tweet, 0, 0);
TabHost.TabSpec tab2 = mFragmentTabHost.newTabSpec("tag2");//添加tab标签
tab2.setIndicator(IndicatorView2);//设置样式
Bundle bundle2 = new Bundle();
bundle2.putString("args", "two");
mFragmentTabHost.addTab(tab2, NewsPagerFragment.class, bundle2);
}
//activity_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">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.app.FragmentTabHost
android:id="@+id/fragmentTabHost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.app.FragmentTabHost>
<ImageButton
android:id="@+id/ibQuickoption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@null"
android:src="@drawable/btn_quickoption_selector"/>
</RelativeLayout>
</LinearLayout>
//view_indicator1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:drawableTop="@drawable/tab_icon_new"
android:gravity="center"
android:text="资讯"
android:textColor="@color/primarybar_txt"
android:textSize="12sp"/>
<TextView
android:id="@+id/tab_mes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@id/tab_title"
android:layout_alignTop="@id/tab_title"
android:layout_marginLeft="1dip"
android:text="10"
android:textColor="#f00"
android:visibility="invisible"/>
</RelativeLayout>
FragmentTabHost底部切换
最新推荐文章于 2018-07-20 16:19:09 发布