网站首页关键如何优化,网站添加提醒,女的和男的做那个视频网站,wordpress登录界面修改TabLayout的使用简单介绍比如在平常的项目中实现这样的效果#xff0c;一般都是都会使用viewPageIndicate等几个开源框架直接实现#xff0c;或者使用自定义的HorizontalScroll再配合ViewPageFragment实现。在谷歌推出marginDesign之后#xff0c;实现这种效果可以直接使用T…TabLayout的使用简单介绍比如在平常的项目中实现这样的效果一般都是都会使用viewPageIndicate等几个开源框架直接实现或者使用自定义的HorizontalScroll再配合ViewPageFragment实现。在谷歌推出marginDesign之后实现这种效果可以直接使用TabLayout实现。另外Tablayout可以通过自定义View自定义导航栏的效果。这样使用的时候更加灵活多变。首先需要导入design包在app的build.gradle下添加design的包dependencies {compile com.android.support:design:25.0.1}然后就开始撸起袖子开始如何使用在xml文件里面写布局xmlns:toolshttp://schemas.android.com/toolsandroid:idid/activity_mainandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalandroid:idid/tabLayoutandroid:layout_widthmatch_parentstylestyle/MyCustomTabLayoutandroid:layout_heightwrap_content /android:idid/viewPagerandroid:layout_widthmatch_parentandroid:layout_height0dpandroid:layout_weight1/既然使用到了fragment就免不了要添加下简单的布局android:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalandroid:idid/textViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_centerInParenttrueandroid:text默认 /然后是fragment和FragmentPagerAdapter的代码。写过的人应该对这个很熟了就直接粘下代码public class FramentAdapter extends FragmentPagerAdapter {private String[] titles;public FramentAdapter(FragmentManager fm, String[] titles) {super(fm);this.titles titles;}Overridepublic Fragment getItem(int position) {return PageFragment.newInstace(position,titles);}Overridepublic int getCount() {return titles.length;}Overridepublic CharSequence getPageTitle(int position) {return titles[position];}}Fragment的代码public class PageFragment extends Fragment {private int position;private String[] titles;private Context context;public static PageFragment newInstace(int position, String[] titles) {Bundle bundle new Bundle();bundle.putInt(POSITION, position);bundle.putStringArray(ARRAY, titles);PageFragment pageFragment new PageFragment();pageFragment.setArguments(bundle);return pageFragment;}Overridepublic void onAttach(Context context) {super.onAttach(context);this.contextcontext;}Overridepublic void onCreate(Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);position getArguments().getInt(POSITION);titles getArguments().getStringArray(ARRAY);}NullableOverridepublic View onCreateView(LayoutInflater inflater, Nullable ViewGroup container, Nullable Bundle savedInstanceState) {View view inflater.inflate(R.layout.item_layout,null);TextView textView (TextView) view.findViewById(R.id.textView);textView.setText(titles[position]);return view;}}写好这些之后最后在MainActivity中看下如何使用public class MainActivity extends FragmentActivity {private TabLayout tabLayout;private ViewPager viewPager;private String[] titles {黄蓉, 郭靖, 杨过, 小龙女, 尹志平, 金轮法王, 收到货就收到货圣诞节};private TextView textView;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabLayout (TabLayout) findViewById(R.id.tabLayout);viewPager (ViewPager) findViewById(R.id.viewPager);FramentAdapter framentAdapter new FramentAdapter(getSupportFragmentManager(), titles);viewPager.setAdapter(framentAdapter);tabLayout.setupWithViewPager(viewPager);tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);}}viewPager.setAdapter(framentAdapter);这行代码必须在下面这行代码之前。有兴趣的可以看下源码。 tabLayout.setupWithViewPager(viewPager);另外 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);给tablayout设置了这种模式mode有两种这种模式大概的意思是我内容很多的时候可以使tab平铺滑动。很多时候我们需要自己自定义样式或者要自定义我们的tab。自定义样式需要在Style文件下添加自己的样式然后应用就好了例如?attr/colorAccent2dp12dp12dp?attr/selectableItemBackgroundstyle/MyCustomTabTextAppearance?android:textColorPrimary14sp?android:textColorSecondarytrue另外一种就是需要添加我们自定义的View首先写要定义的布局文件android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:orientationhorizontalandroid:idid/textViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:gravitycenter/android:layout_width10dpandroid:layout_height10dpandroid:backgrounddrawable/bg_text /然后稍微修改下FragmentPagerAdapter的代码public class FramentAdapter extends FragmentPagerAdapter {private String[] titles;public FramentAdapter(FragmentManager fm, String[] titles) {super(fm);this.titles titles;}Overridepublic Fragment getItem(int position) {return PageFragment.newInstace(position,titles);}Overridepublic int getCount() {return titles.length;}Overridepublic CharSequence getPageTitle(int position) {return null;}}最后看下怎么在MainActivity中使用。public class MainActivity extends FragmentActivity {private TabLayout tabLayout;private ViewPager viewPager;private String[] titles {黄蓉, 郭靖, 杨过, 小龙女, 尹志平, 金轮法王, 收到货就收到货圣诞节};private TextView textView;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabLayout (TabLayout) findViewById(R.id.tabLayout);viewPager (ViewPager) findViewById(R.id.viewPager);FramentAdapter framentAdapter new FramentAdapter(getSupportFragmentManager(), titles);viewPager.setAdapter(framentAdapter);tabLayout.setupWithViewPager(viewPager);tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);for (int i 0; i tabLayout.getTabCount(); i) {TabLayout.Tab tab tabLayout.getTabAt(i);tab.setCustomView(getTabView(i));}}private View getTabView(int position) {View view View.inflate(this, R.layout.item_tab_view, null);textView (TextView) view.findViewById(R.id.textView);textView.setText(titles[position]);return view;}}到这里就结束了TabLayout的使用。放下源码Tablayout的使用以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。