Tablayout的使用
来源:互联网 发布:网络剧投资 编辑:程序博客网 时间:2024/06/08 07:40
Fragment+ViewPager+FragmentPagerAdapter的方式
activity的布局:
<?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.zdl.testapp.TabActivity"> <android.support.design.widget.TabLayout android:id="@+id/tb" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" /> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>
使用之前一定要先引入design库
初始化操作
viewPager = (ViewPager) findViewById(R.id.vp); tabLayout = (TabLayout) findViewById(R.id.tb);
Fragment布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
public class PageFragment extends Fragment { public static final String ARGS_PAGE = "args_page"; private int mPage; /** * 单例模式防止重复创建 * @param page * @return */ public static PageFragment newInstance(int page) { Bundle args = new Bundle(); args.putInt(ARGS_PAGE, page); PageFragment pageFragment = new PageFragment(); pageFragment.setArguments(args); return pageFragment; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPage = getArguments().getInt(ARGS_PAGE); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmnt_layout, null, false); TextView textView = (TextView) view.findViewById(R.id.tv); textView.setText("第" + mPage + "页"); return view; }}
adapter:
public class FragmentAdapter extends FragmentPagerAdapter { private String[] titles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4", "Tab5"}; private Context context; public FragmentAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; } /** * 获取fragment * * @param position * @return */ @Override public Fragment getItem(int position) { return PageFragment.newInstance(position + 1); } /** * 标题长度 * * @return */ @Override public int getCount() { return titles.length; } @Override public CharSequence getPageTitle(int position) { return titles[position]; }}
tablayout+viewpager一起使用
FragmentPagerAdapter fragmentPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), this); viewPager.setAdapter(fragmentPagerAdapter); tabLayout.setupWithViewPager(viewPager);
tab没有占满的情况
1.代码
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setTabMode(TabLayout.MODE_FIXED);2布局
<android.support.design.widget.TabLayout android:id="@+id/tabLayout" app:tabGravity="fill" app:tabMode="fixed" android:layout_width="match_parent" android:layout_height="wrap_content" />
阅读全文
0 0
- TabLayout的简单使用(TabLayout+Fragment+ViewPager)
- TabLayout的简单使用
- TabLayout的使用
- TabLayout的使用分享
- TabLayout的使用
- 关于TabLayout的使用
- tabLayout 的初步使用
- TabLayout的使用
- TabLayout的使用
- TabLayout的使用
- TabLayout的简单使用
- 25.TabLayout的使用
- TabLayout的使用
- TabLayout的基础使用
- TabLayout的使用
- TabLayout的简单使用
- TabLayout的使用
- TabLayout的简单使用
- [LeetCode]--Container With Most Water
- 关于listview中是用多个布局 并使用holderView 缓存机制的问题
- 针对json的查询--alibaba的开源项目jsonq
- Spring源码分析的几篇博客
- 解决NPM使用下载慢或者无法下载的问题error
- Tablayout的使用
- socket 并发服务器 代码案例 (多进程模式)
- spring boot学习(六)---表单验证
- 进程通信之无名pipe
- 第78讲 面向对象编程综合练习
- 基于nginx的中间件架构(1)环境准备
- __bridge 显式转换 id和void *
- bzoj 5047 空间传送装置 最短路
- 在Mac中配置tomcat服务器教程(超详细配图)以及其配置简单原理