您现在的位置是:首页 > 文章详情

TabLayout的简单使用

日期:2018-08-08点击:328

什么是TabLayout

TabLayout是Support Design Library库中的一个控件,它是用来进行分组的,同时也可以作为ViewPager的指示器

TabLayout的简单使用

●tab创建

◇xml布局创建
<android.support.design.widget.TabLayout android:layout_width="match_parent" android:id="@+id/tablayout" android:layout_height="wrap_content"> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:text="tab1" //设置tab显示的文字 android:icon="@mipmap/ic_launcher" //设置tab的图片 android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:text="tab2" android:icon="@mipmap/ic_launcher" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:text="tab3" android:icon="@mipmap/ic_launcher" android:layout_height="wrap_content" /> </android.support.design.widget.TabLayout> 
◇Java代码动态创建
tabLayout=findViewById(R.id.tablayout); tabLayout.addTab(tabLayout.newTab().setText("标签1").setIcon(R.mipmap.ic_launcher)); tabLayout.addTab(tabLayout.newTab().setText("标签2").setIcon(R.mipmap.ic_launcher)); tabLayout.addTab(tabLayout.newTab().setText("标签3").setIcon(R.mipmap.ic_launcher)); 
img_f2e03c684d5ebd96a891c79b89761583.gif

●tab的布局排版

tab的布局排版受两个属性限制app:tabMode【fixed(固定)、scrollable(滚动))】和app:tabGravity(fill(填充)、center(居中))

◇设置app:tabMode=“fixed”然后设置app:tabGravity分别为fill和center时的效果

1)app:tabGravity="fill"

<android.support.design.widget.TabLayout android:layout_width="match_parent" android:id="@+id/tablayout" app:tabMode="fixed" app:tabGravity="fill" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> 
img_0854192561d85db8f5f71721835df37a.png

2)app:tabGravity="center"

<android.support.design.widget.TabLayout android:layout_width="match_parent" android:id="@+id/tablayout" app:tabMode="fixed" app:tabGravity="center" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> 
img_2bebc24fe058dbe95dd4b002949e6358.png

◇设置app:tabMode=“scrollable”然后设置app:tabGravity分别为fill和center时的效果

1)app:tabGravity="fill"

<android.support.design.widget.TabLayout android:layout_width="match_parent" android:id="@+id/tablayout" app:tabMode="scrollable" app:tabGravity="fill" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> 
img_e71e0c977a2560c807f9384cc1f2f533.png

2)app:tabGravity="center"

<android.support.design.widget.TabLayout android:layout_width="match_parent" android:id="@+id/tablayout" app:tabMode="scrollable" app:tabGravity="center" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> 
img_4416e990966aefe6c00d695b4d0839b7.png

●其他设置

<android.support.design.widget.TabLayout android:layout_width="match_parent" android:id="@+id/tablayout" app:tabMode="scrollable" app:tabGravity="center" app:tabIndicatorHeight="5dp" //设置指示线高度,如果想要隐藏指示线可以通过将该属性设置为0dp实现 app:tabIndicatorColor="@color/colorPrimary" //设置指示线颜色 app:tabTextColor="@color/colorPrimary" //设置未选中文字颜色 app:tabSelectedTextColor="@color/colorRed" //设置选中文字颜色 android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> 
img_22578bff2b7a6e0febdfceabc82841cc.gif

●绑定ViewPager

这里tab的Text是通过PagerAdapter的getPageTitle方法获取的

public class TabLayoutSimpleActivity2 extends AppCompatActivity { ViewPager viewPager; TabLayout tabLayout; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tablayout_activitysimple); viewPager=findViewById(R.id.vp); tabLayout=findViewById(R.id.tablayout); viewPager.setAdapter(new PagerAdapter() { @Override public int getCount() { return 5; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { TextView textView=new TextView(TabLayoutSimpleActivity2.this); textView.setTextSize(50); textView.setText(position+""); container.addView(textView); return textView; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @Nullable @Override public CharSequence getPageTitle(int position) { return "标签"+position; } }); tabLayout.setupWithViewPager(viewPager); } } 
img_602b65e79d44247c5a33ad522edea6ed.gif

个人博客:https://myml666.github.io

原文链接:https://yq.aliyun.com/articles/665565
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章