Android5.0材料设计(二)
2016-01-18 23:30
459 查看
()TabLayout
TabLayout和ViewPager是最佳组合
()实现方式
布局
app:tabIndicatorColor 设置TabLayout中tab选中时指标(Indicator)的颜色
app:tabMinWidth 设置TabLayout中tab的最少宽度
app:tabMode 设置TabLayout能否滚动,scrollable表示可以滚动tab,fixed表示不可以滚动tab
app:tabSelectedTextColor 设置TabLayout中tab选中时字体的颜色
app:tabGravity 设置TabLayout的位置,center表示所有的tab在屏幕的中间,fill表示所有的可用空间给每个tab。如果 tabMode 是设置成 scrollable 的,则这个属性将会被忽略
Activity
Adapter
Fragment
可以给TabLayout设置一些Listener:
OnTabSelectedListener - Tab被选中时,触发的Listener
TabLayoutOnPageChangeListener
ViewPagerOnTabSelectedListener
TabLayout和ViewPager是最佳组合
()实现方式
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="50dp"> <android.support.design.widget.TabLayout android:id="@+id/tab_tl" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|enterAlways" app:tabIndicatorColor="#df4138" app:tabMinWidth="100dp" app:tabMode="scrollable" app:tabSelectedTextColor="#df4138" app:tabTextColor="#333333" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="bottom" android:background="#cccccc" /> </FrameLayout> <android.support.v4.view.ViewPager android:id="@+id/tab_vp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@android:color/white" /> </LinearLayout>
app:tabIndicatorColor 设置TabLayout中tab选中时指标(Indicator)的颜色
app:tabMinWidth 设置TabLayout中tab的最少宽度
app:tabMode 设置TabLayout能否滚动,scrollable表示可以滚动tab,fixed表示不可以滚动tab
app:tabSelectedTextColor 设置TabLayout中tab选中时字体的颜色
app:tabGravity 设置TabLayout的位置,center表示所有的tab在屏幕的中间,fill表示所有的可用空间给每个tab。如果 tabMode 是设置成 scrollable 的,则这个属性将会被忽略
Activity
ViewPager tabVp = (ViewPager) findViewById(R.id.tab_vp); TabPagAdapter adapter = new TabPagAdapter(getSupportFragmentManager(), this); tabVp.setAdapter(adapter); //自动调用ViewPager mTabLayout.setupWithViewPager(tabVp);
Adapter
private String tabTitles[] = new String[]{"Tab1", "Tab2", "Tab3", "Tab4", "Tab5", "Tab6", "Tab7", "Tab8", "Tab9"}; private Context context; public TabPagAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; } @Override public Fragment getItem(int position) { return TabFragment.newInstance(position + 1); } @Override public int getCount() { return tabTitles.length; } @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; }
Fragment
public static final String ARG_PAGE = "ARG_PAGE"; private int mPage; public static TabFragment newInstance(int page) { Bundle args = new Bundle(); args.putInt(ARG_PAGE, page); TabFragment fragment = new TabFragment(); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPage = getArguments().getInt(ARG_PAGE); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_page, container, false); TextView textView = (TextView) view; textView.setText("Fragment #" + mPage); return view; }
可以给TabLayout设置一些Listener:
OnTabSelectedListener - Tab被选中时,触发的Listener
TabLayoutOnPageChangeListener
ViewPagerOnTabSelectedListener
相关文章推荐
- Android5.0材料设计(一)
- 我的Android进阶之旅------>关于使用CSDN-markdown编辑器来编写博客
- Android开发从零开始(1)--2016.01.18
- Android Activity Fragment 生命周期
- 安装谷大家的Ingress(Android)
- mac 上 配置 Android NDK 环境
- Android Apk 代码混淆与资源文件混淆实战
- Android 快速开发 打造万能的ListView GridView 适配器
- 极光推送之服务器端向android等客户端推送实例
- Android apk集成
- Android ORM框架之GreenDao的使用方法
- ArcGIS4Android 2:多个安卓module共享jar和so文件
- Android开启StrictMode模式
- android中的sp和wp
- 51.Rxjava+RxAndroid+Retrofit组合问题
- 【android基础三】Android中SQLite应用详解
- Android学习小结3(活动)
- Android学习小结2
- 读书笔记1.Android启动模式
- Android笔记--对JavaBean的理解和AlerDialog的使用。