这个是整体布局xml布局(重点是红色部分,那个是栏目的布局)
<?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">
<LinearLayout
android:background="#d23d3d"
android:layout_width="match_parent"
android:layout_height="30dp">
</LinearLayout>
<FrameLayout
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这个是我在界面实现的逻辑(我是Fragment里面实现)
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.columndotplayerfragment, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
mInfos = new ArrayList<>();
//这个是String资源里面的数据,
String[] titls = getResources().getStringArray(R.array.news_titles);//修改里面的数据就可以修改栏目的名字
//里面主要是栏目切换的Fragment for (int i = 0; i < titls.length; i++) { String title = titls[i]; FragmentInfo info; if (i == 0) { info = new FragmentInfo(new DotPlayerFragment(), title); } else if (i == 1) { info = new FragmentInfo(new ConferenceFragment(), title); } else if (i == 2) { info = new FragmentInfo(new TrainFragment(), title); } else if (i == 3) { info = new FragmentInfo(new MediaFragment(), title); } else if (i == 4) { info = new FragmentInfo(new OfficialFragment(), title); } else if (i == 5) { info = new FragmentInfo(new BackstageFragment(), title); } else { info = new FragmentInfo(new OutPutLivePlayerFragment(), title); } mInfos.add(info); }
//下面的代码 FrameLayout tabs = (FrameLayout) view.findViewById(R.id.tab);
tabs.addView(LayoutInflater.from(getContext()).inflate(R.layout.include_tabs, tabs, false));//include_tabs布局主要是修改栏目的UI比如染色,字体大小(),把布局放进tabs里面显示
SmartTabLayout viewpagerTab = (SmartTabLayout) view.findViewById(R.id.viewpagerTab); viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(), mInfos); viewPager.setAdapter(viewPagerAdapter); viewpagerTab.setViewPager(viewPager); return view; }
include_tabs的布局
<?xml version="1.0" encoding="utf-8"?> <com.ogaclejapan.smarttablayout.SmartTabLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/viewpagerTab" android:background="#d64848" android:layout_width="match_parent" android:layout_height="50dp" app:stl_customTabTextLayoutId="@layout/item_tab" app:stl_customTabTextViewId="@+id/custom_texts" app:stl_indicatorColor="@color/red_text" app:stl_indicatorInterpolation="linear" app:stl_indicatorThickness="3dp" app:stl_underlineThickness="1dp" />