android design库之TabLayout的用法
来源:互联网 发布:linux 禅道下载地址 编辑:程序博客网 时间:2024/06/05 22:38
1.在gradle引入design库
compile 'com.android.support:appcompat-v7:23.4.0'compile 'com.android.support:design:23.4.0'
2.主页面布局
<?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" > <android.support.design.widget.TabLayout android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" android:background="@android:color/white" /></LinearLayout>
3.adapter布局
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" />
4.adapter代码
public class SimpleFragmentAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 3; private String tabTitles[] = new String[]{"tabl","tab2","tab2"}; private Context context; public SimpleFragmentAdapter(FragmentManager fm,Context context) { super(fm); this.context = context; } @Override public Fragment getItem(int position) { return fragment.getInstance(position); } @Override public int getCount() { return PAGE_COUNT; } @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; }}
5.framgent代码
public class fragment extends Fragment { public static String tag = "fragment"; private int num; public static fragment getInstance(int num){ Bundle bundle = new Bundle(); bundle.putInt(tag,num); fragment fg = new fragment(); fg.setArguments(bundle); return fg; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); num = getArguments().getInt(tag); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, container, false); TextView tv = (TextView)view; tv.setText("Fragment++"+num); return view; }
6.MainActivity代码
public class MainActivity extends AppCompatActivity { private SimpleFragmentAdapter fragmentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentAdapter = new SimpleFragmentAdapter(getSupportFragmentManager(), this); ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(fragmentAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); tabLayout.setTabMode(TabLayout.MODE_FIXED); }}7,综上所述就实现了TabLayout最简单的用法了。
0 0
- android design库之TabLayout的用法
- Android Design Support Library - TabLayout的用法
- Android Support Design库之TabLayout
- Android中Design库之TabLayout
- Android Material Design 之 TabLayout
- android design library提供的TabLayout的用法
- android design library提供的TabLayout的用法
- android design library提供的TabLayout的用法
- android design library提供的TabLayout的用法
- android design library提供的TabLayout的基本用法
- android design library提供的TabLayout的用法
- Android Design Library(一)提供的TabLayout的用法
- android design library提供的TabLayout的用法
- android design library提供的TabLayout的用法
- Material Design之TabLayout的用法(标题栏滑动+ViewPager)
- Android Material Design控件——TabLayout的用法
- android.support.design.widget.TabLayout 用法解析
- android Material Design之 ToolBar+TabLayout+recycleView的效果实现
- 第34课:在IDEA中开发Spark实战
- QGroundControl笔记 —— 界面启动
- OC - 网络图片压缩,获取网络图片尺寸
- ViewPagerIndicator的指示器应用
- 京东前端在线笔试
- android design库之TabLayout的用法
- Linux内核启动参数的设置
- 公共POI导出Excel方法--java
- Spring使用Cache、整合Ehcache
- 网络请求及各类错误代码含义总结(包含AFN错误码大全)
- 设计模式-代理模式
- Linux下配置 Keepalived(心跳检测部署)
- eclipse 创建工程 appcompat_v7 报错
- [sql]根据多个字段去重