第一步:布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.admin.fragmentdemo1.MainActivity"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <android.support.v4.app.FragmentTabHost android:id="@+id/tabHost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#aaaaaa"> </android.support.v4.app.FragmentTabHost> </LinearLayout>
第二步:新建布局文件tab_item_view.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="30dp" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/texyView1" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" android:text="首页" android:textSize="10dp" android:textColor="#000000" android:background="@drawable/selector"/> </LinearLayout> 第三步:添加选中与未选中的样式
在drawable新建selector.xml文件
<?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/isselector"></item> <item android:state_selected="false" android:drawable="@drawable/unselector"></item> </selector>
选中状态,新建isselector.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ddd" /> </shape>
未选中状态,新建unselector.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#fff"/> </shape>
第四步:新建fragment_1.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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123" android:layout_gravity="center"/> </LinearLayout>
第五步:新建类文件继承Fragment(android.support.v4.app)需要几个新建几个
public class FragmentPage1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_1,null); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }
第六步:MainActivity中代码
public class MainActivity extends AppCompatActivity { private FrameLayout frameLayout; private LayoutInflater layoutInflater; private FragmentTabHost tabHost; private Context context; private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class}; private String textViewArray[] = {"直播","动画片","电影","音乐","我的"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; layoutInflater = LayoutInflater.from(context); tabHost = findViewById(R.id.tabHost); tabHost.setup(context,getSupportFragmentManager(),R.id.realtabcontent); for(int i = 0;i<fragmentArray.length;i++) { TabHost.TabSpec tabSpec = tabHost.newTabSpec(textViewArray[i]).setIndicator(getTabItemView(i)); tabHost.addTab(tabSpec,fragmentArray[i],null); } } private View getTabItemView(int index) { View view = layoutInflater.inflate(R.layout.tab_item_view,null); TextView textView = view.findViewById(R.id.texyView1); textView.setText(textViewArray[index]); return view; } }
如有看不懂,欢迎留言;如有错误,欢迎指正;如果哪位大神有好的方法,欢迎分享。