您的位置:首页 > 移动开发 > Android开发

android 切换栏目 SmartTabLayout的使用

2017-07-13 10:32 881 查看
这个是整体布局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);
//include_tabs布局主要是修改栏目的UI比如染色,字体大小(),把布局放进tabs里面显示
tabs.addView(LayoutInflater.from(getContext()).inflate(R.layout.include_tabs, tabs, false));

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"
/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: