全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android MarginDesign控件TabLayout导航栏使用详解

TabLayout的使用简单介绍

比如在平常的项目中实现这样的效果,一般都是都会使用viewPageIndicate等几个开源框架直接实现,或者使用自定义的HorizontalScroll再配合ViewPage+Fragment实现。在谷歌推出marginDesign之后,实现这种效果可以直接使用TabLayout实现。另外Tablayout可以通过自定义View自定义导航栏的效果。这样使用的时候更加灵活多变。

首先需要导入design包

在app的build.gradle下添加design的包

dependencies {
  compile 'com.android.support:design:25.0.1'
 }

然后就开始撸起袖子,开始如何使用

在xml文件里面写布局:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
  android:id="@+id/tabLayout"
  android:layout_width="match_parent"
  style="@style/MyCustomTabLayout"
  android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
  android:id="@+id/viewPager"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
  />
</LinearLayout>

既然使用到了fragment,就免不了要添加下简单的布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
  android:id="@+id/textView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:text="默认" />
 </RelativeLayout>

然后是fragment和FragmentPagerAdapter的代码。写过的人应该对这个很熟了,就直接粘下代码

public class FramentAdapter extends FragmentPagerAdapter {
private String[] titles;

public FramentAdapter(FragmentManager fm, String[] titles) {
  super(fm);
  this.titles = titles;
}

@Override
public Fragment getItem(int position) {
  return PageFragment.newInstace(position,titles);
}

@Override
public int getCount() {
  return titles.length;
}

@Override
public 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;
}

@Override
public void onAttach(Context context) {
  super.onAttach(context);
  this.context=context;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  position = getArguments().getInt("POSITION");
  titles = getArguments().getStringArray("ARRAY");
}

@Nullable
@Override
public 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;

@Override
protected 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文件下添加自己的样式,然后应用就好了,例如;

<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
  <item name="tabIndicatorColor">?attr/colorAccent</item>
  <item name="tabIndicatorHeight">2dp</item>
  <item name="tabPaddingStart">12dp</item>
  <item name="tabPaddingEnd">12dp</item>
  <item name="tabBackground">?attr/selectableItemBackground</item>
  <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
  <item name="tabSelectedTextColor">?android:textColorPrimary</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
  <item name="android:textSize">14sp</item>
  <item name="android:textColor">?android:textColorSecondary</item>
  <item name="textAllCaps">true</item>
</style>

另外一种就是需要添加我们自定义的View:
首先写要定义的布局文件;

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
  android:id="@id/textView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  />

<TextView
  android:layout_width="10dp"
  android:layout_height="10dp"
  android:background="@drawable/bg_text" />
</LinearLayout>

然后稍微修改下FragmentPagerAdapter的代码

 public class FramentAdapter extends FragmentPagerAdapter {
private String[] titles;

public FramentAdapter(FragmentManager fm, String[] titles) {
  super(fm);
  this.titles = titles;
}

@Override
public Fragment getItem(int position) {
  return PageFragment.newInstace(position,titles);
}

@Override
public int getCount() {
  return titles.length;
}

@Override
public CharSequence getPageTitle(int position) {
  return null;
}
}

最后看下怎么在MainActivity中使用。

public class MainActivity extends FragmentActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private String[] titles = {"黄蓉", "郭靖", "杨过", "小龙女", "尹志平", "金轮法王", "收到货就收到货圣诞节"};
private TextView textView;

@Override
protected 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的使用

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Android  # TabLayout  # 导航栏  # TabLayout使用方法详解  # Android使用TabLayout+Fragment实现顶部选项卡  # android TabLayout使用方法详解  # Tablayout简单使用方法总结  # 自定义  # 如何使用  # 杨过  # 这行  # 自己的  # 的人  # 就收  # 都是  # 金轮  # 几个  # 平铺  # 可以通过  # 有兴趣  # 可以直接  # 有两种  # 如在  # 写过  # 写好  # 开源  # 大家多多 


相关文章: Avalonia如何实现跨窗口通信 Avalonia窗口间数据传递  如何快速搭建虚拟主机网站?新手必看指南  制作公司内部网站有哪些,内网如何建网站?  ui设计制作网站有哪些,手机UI设计网址吗?  专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?  如何在Golang中指定模块版本_使用go.mod控制版本号  建站上市公司网站建设方案与SEO优化服务定制指南  如何通过多用户协作模板快速搭建高效企业网站?  山东云建站价格为何差异显著?  如何在阿里云完成域名注册与建站?  如何快速配置高效服务器建站软件?  儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?  已有域名和空间如何搭建网站?  如何高效配置IIS服务器搭建网站?  电商平台网站制作流程,电商网站如何制作?  网站微信制作软件,如何制作微信链接?  昆明网站制作哪家好,昆明公租房申请网上登录入口?  如何选择高性价比服务器搭建个人网站?  简历在线制作网站免费,免费下载个人简历的网站是哪些?  建站之星2.7模板快速切换与批量管理功能操作指南  建站之星备案流程有哪些注意事项?  如何在Golang中处理模块冲突_解决依赖版本不兼容问题  制作门户网站的参考文献在哪,小说网站怎么建立?  如何通过老薛主机一键快速建站?  昆明高端网站制作公司,昆明公租房申请网上登录入口?  台州网站建设制作公司,浙江手机无犯罪记录证明怎么开?  IOS倒计时设置UIButton标题title的抖动问题  如何高效完成独享虚拟主机建站?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  阿里云网站制作公司,阿里云快速搭建网站好用吗?  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  如何用美橙互联一键搭建多站合一网站?  如何在Golang中使用replace替换模块_指定本地或远程路径  如何选择适配移动端的WAP自助建站平台?  php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】  如何通过虚拟主机快速完成网站搭建?  ,想在网上投简历,哪几个网站比较好?  制作网站的公司有哪些,做一个公司网站要多少钱?  如何在阿里云服务器自主搭建网站?  合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  网站建设制作、微信公众号,公明人民医院怎么在网上预约?  如何在阿里云虚拟主机上快速搭建个人网站?  jQuery 常见小例汇总  重庆网站制作公司哪家好,重庆中考招生办官方网站?  网站代码制作软件有哪些,如何生成自己网站的代码?  建站之星后台密码遗忘?如何快速找回?  建站之星ASP如何实现CMS高效搭建与安全管理? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。