Android_TabLyout的使用
来源:互联网 发布:mac socket 调试工具 编辑:程序博客网 时间:2024/06/16 19:14
//根据版本添加
//添加导包 com.android.support:design:26.0.0-alpha1
//Mainactivity内容
import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private List<String> datas = new ArrayList<String>(); private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//获取控件 tabLayout = (TabLayout) findViewById(R.id.tas); viewPager = (ViewPager) findViewById(R.id.vp);//横向滑动的集合 datas.add("推荐"); datas.add("要闻"); datas.add("娱乐"); datas.add("科技"); datas.add("汽车"); datas.add("体育"); datas.add("图片"); datas.add("动漫"); datas.add("社会"); datas.add("游戏");//适配器 vpsp vpsp = new vpsp(getSupportFragmentManager()); viewPager.setAdapter(vpsp);//进行关联 tabLayout.setupWithViewPager(viewPager);// tabLayout.setupWithViewPager(viewPager);// tabLayout.setTabsFromPagerAdapter(vpsp); //虽然过时了但是不能去掉,去掉后 //如果是滑动操作的话没事,但是使用标签点击的时候就不行了。// tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); //tab的模式如果标签多的话用MODE_SCROLLABLE 少的话用MODE_FIXED } class vpsp extends FragmentPagerAdapter{//有参数的构造 public vpsp(FragmentManager fm) { super(fm); }//返回选项卡的文本 ,,,添加选项卡 @Override public CharSequence getPageTitle(int position) { return datas.get(position); }//创建fragment对象并返回 @Override public Fragment getItem(int position) { content content = new content(); Bundle bundle = new Bundle(); bundle.putString("name",datas.get(position)); content.setArguments(bundle); return content; }//返回数量 @Override public int getCount() { return datas.size(); } }}
//Main的布局
<?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" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.chenxu20171014.MainActivity"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/tas" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" > </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" android:layout_below="@+id/tas" > </android.support.v4.view.ViewPager></LinearLayout>//content的内容public class content extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = View.inflate(getActivity(), R.layout.contentlayout, null); TextView cte = inflate.findViewById(R.id.cte); Bundle arguments = getArguments(); String name = arguments.getString("name"); Log.e("chen", "onCreateView: ------"+name ); cte.setText(name); return inflate; }}//contentlayout的布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:id="@+id/cte" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dfxcvnxcv" android:textColor="@color/colorAccent"/></LinearLayout>
阅读全文
0 0
- Android_TabLyout的使用
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- 使用RocketMQ的客户端使用
- gulp的使用基本使用
- list集合的contains问题
- 【JSON】生成JSON数据的方式
- hdu 1285 确定比赛名次
- 基于概率论的分类方法:朴素贝叶斯及CSDN_RSS源分析
- Android Hugo 插件
- Android_TabLyout的使用
- Delphi 关键字详解
- 程不识传笔记
- 欢迎使用CSDN-markdown编辑器
- POJ 2356 Find a multiple
- SpringBoot进阶之统一接口返回信息格式
- [c] windows下编译boost 1.55.0
- 思想才是王道之sql与递归
- 数据结构 P42-43 算法实现 多项式的建立及加法