<?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/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/bg_color"/> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
public class MainActivity extends AppCompatActivity {
private LayoutInflater mInflater;
private FragmentTabHost mTabHost;
private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInflater = LayoutInflater.from(this);
mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
//第一步,初始化fTabHost, 第三个参数为内容容器
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//第二步,初始化菜单项
TabHost.TabSpec tabSpec = mTabHost.newTabSpec("主页");
/*添加菜单项布局*/
View view = mInflater.inflate(R.layout.tab_indicator, null);
ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon);
TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text);
iconTab.setImageResource(R.drawable.tab_home_normal);
tvTab.setText("主页");
tabSpec.setIndicator(view);
//第三步,添加菜单项和内容
mTabHost.addTab(tabSpec, HomeFragment.class, null);
// initTabHost();
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="3dp" android:paddingBottom="3dp" android:layout_gravity="center" android:gravity="center"> <ImageView android:id="@+id/iv_tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/tabTextColor" android:layout_marginTop="2dp"/> </LinearLayout>
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, null);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/themeColor" android:text="@string/tabHome"/> </LinearLayout>
public class TabDataBean {
private int tabName;
private int tabIcon;
private Class content; //对应的内容类
public TabDataBean(int tabName, int tabIcon, Class content) {
this.tabName = tabName;
this.tabIcon = tabIcon;
this.content = content;
}
public int getTabName() {
return tabName;
}
public void setTabName(int tabName) {
this.tabName = tabName;
}
public int getTabIcon() {
return tabIcon;
}
public void setTabIcon(int tabIcon) {
this.tabIcon = tabIcon;
}
public Class getContent() {
return content;
}
public void setContent(Class content) {
this.content = content;
}
}
private void initTabHost() {
//初始化fTabHost, 第三个参数为内容容器
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
/*初始化数据源*/
TabDataBean bean = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class);
//添加底部菜单项-tabSpec
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));
//给菜单项添加内容,indicator,其中indicator需要的参数View即为菜单项的布局
tabSpec.setIndicator(getIndicatorView(bean));
//第二参数就是该菜单项对应的页面内容
mTabHost.addTab(tabSpec, bean.getContent(), null)
}
private View getIndicatorView(TabDataBean bean){
View view = mInflater.inflate(R.layout.tab_indicator, null);
ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon);
TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text);
iconTab.setImageResource(bean.getTabIcon());
tvTab.setText(bean.getTabName());
return view;
}
public class MainActivity extends AppCompatActivity {
private LayoutInflater mInflater;
private FragmentTabHost mTabHost;
private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInflater = LayoutInflater.from(this);
mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
initTabHost();
}
/**
* 初始化底部导航栏
*/
private void initTabHost() {
//初始化fTabHost, 第三个参数为内容容器
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
/*初始化数据源*/
TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class);
TabDataBean tabHot = new TabDataBean(R.string.tabHot, R.drawable.tab_life_normal, HotFragment.class);
TabDataBean tabCategory = new TabDataBean(R.string.tabCategory, R.drawable.tab_service_normal, CategoryFragment.class);
TabDataBean tabCart = new TabDataBean(R.string.tabCart, R.drawable.tab_order_normal, CartFragment.class);
TabDataBean tabMine = new TabDataBean(R.string.tabMine, R.drawable.tab_mine_normal, MineFragment.class);
tabDataList.add(tabHome);
tabDataList.add(tabHot);
tabDataList.add(tabCategory);
tabDataList.add(tabCart);
tabDataList.add(tabMine);
//添加底部菜单项-tabSpec
for (TabDataBean bean : tabDataList) {
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));
//给菜单项添加内容,indicator,其中indicator需要的参数View即为菜单项的布局
tabSpec.setIndicator(getIndicatorView(bean));
//第二参数就是该菜单项对应的页面内容
mTabHost.addTab(tabSpec, bean.getContent(), null);
}
}
/**
* 初始化indciator的内容
* @param bean
*/
private View getIndicatorView(TabDataBean bean){
View view = mInflater.inflate(R.layout.tab_indicator, null);
ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon);
TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text);
iconTab.setImageResource(bean.getTabIcon());
tvTab.setText(bean.getTabName());
return view;
}
}
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/tab_home_selected"/> <item android:state_pressed="true" android:drawable="@drawable/tab_home_selected"/> <item android:drawable="@drawable/tab_home_normal"/> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/themeColor" android:state_selected="true"/> <item android:color="@color/themeColor" android:state_active="true"/> <item android:color="@color/tabTextColor" android:state_selected="false"/> <item android:color="@color/tabTextColor" android:state_active="false"/> </selector>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有