package com.example.administrator.myapplication; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends FragmentActivity { private FragmentTabHost tabHost; private List<Fragment>fragmentList=new ArrayList<>(); private int[] imgSelectors=new int[]{ R.drawable.ic_home_tab_index_selector, R.drawable.ic_home_tab_near_selector, R.drawable.ic_home_tab_my_selector, R.drawable.ic_home_tab_more_selector, }; private String[] TabTitle=new String[]{"首页","周边","我的","设置"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentList.add(new BlankFragment()); fragmentList.add(new BlankFragment2()); fragmentList.add(new BlankFragment3()); fragmentList.add(new BlankFragment4()); tabHost = (FragmentTabHost) findViewById(R.id.TabHost); tabHost.setup(MainActivity.this,getSupportFragmentManager(),android.R.id.tabcontent); for (int i=0;i<fragmentList.size();i++){ View view=getLayoutInflater().inflate(R.layout.tab_item,null); ImageView iv= (ImageView) view.findViewById(R.id.iv); TextView tv= (TextView) view.findViewById(R.id.tv); iv.setImageResource(imgSelectors[i]); tv.setText(TabTitle[i]); tabHost.addTab(tabHost.newTabSpec(TabTitle[i]).setIndicator(view),fragmentList.get(i).getClass(),null); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.myapplication.MainActivity"> <android.support.v4.app.FragmentTabHost android:id="@+id/TabHost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <!--<FrameLayout--> <!--android:id="@+id/realContent"--> <!--android:layout_weight="1"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="0dp"></FrameLayout>--> <FrameLayout android:id="@android:id/tabcontent" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"></FrameLayout> <TabWidget android:divider="@null" android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"></TabWidget> </LinearLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
<?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 android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginBottom="5dp" android:orientation="vertical"> <ImageView android:id="@+id/iv" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/ic_home_tab_index_selected" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:textColor="@drawable/text_selector" android:textSize="12sp" /> </LinearLayout> </LinearLayout>
FragmentTabHost简单实用
最新推荐文章于 2025-07-27 16:11:49 发布