TabLayout的基本使用
16lz
2021-01-26
TabLayout
首先供上Google的源码文档https://developer.android.google.cn/reference/android/support/design/widget/TabLayout.html
先放一个例子,这里是用xml配置
<?xml version="1.0" encoding="utf-8"?>"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"> .support.design.widget.TabLayout android:id="@+id/tablayout_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff000000" app:tabIndicatorColor="#fffffff" app:tabIndicatorHeight="10dp" app:tabMode="scrollable" app:tabSelectedTextColor="#ffffffff" app:tabTextColor="#99ffffff" > .support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个Item" /> .support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个Item" /> .support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@mipmap/ic_launcher"/> .support.design.widget.TabLayout>
首先是xml的属性设置
常用的有:
tabMode 有两个供选择
fixed:所有的item全部挤在一起,即所有的item都会在界面中显示出来
scrollable:item按照他们自己的长度来显示,如果总长度超过了界面长度,则可进行滚动(常用的就是这个吧)
item的长度按照下面的两个属性进行限制
tabMaxWidth 每个item的最大长度
tabMinWidth 每个item的最小长度
tabIndicatorColor 设置指示器的颜色,也就是选中item后在下方会有一个横线指示器,可以对其进行设置
tabIndicatorHeight 设置指示器的高度
tabTextColor 所有item的文字的颜色
tabSelectedTextColor 选中的item的文字的颜色(覆盖tabTextColor)
TabItem 用这个来放item,当然也可以在java代码里创建
接下来在看用java代码怎样创建item
public class TestActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.tablayout_test); TabLayout tabLayout= (TabLayout) findViewById (R.id.tablayout_test); tabLayout.addTab (tabLayout.newTab ().setText ("这是第三个Item")); tabLayout.addTab (tabLayout.newTab ().setText ("这是第四个Item")); tabLayout.addTab (tabLayout.newTab ().setText ("这是第五个Item")); tabLayout.addTab (tabLayout.newTab ().setIcon (R.mipmap.ic_launcher)); }}
没错-。-就这么简单,一句代码就ok
然后就是对每个item的点击事件操作
tabLayout.addOnTabSelectedListener (new TabLayout.OnTabSelectedListener () { @Override public void onTabSelected(TabLayout.Tab tab) { Toast.makeText (TestActivity.this,tab.getText ().toString ()+"",Toast.LENGTH_SHORT).show (); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } });
OK,这样,整个TabLayout就已经做好了
更多相关文章
- Golang MD5算法与 java(android)的互通
- Android设置中的Preferencescreen用法介绍与分析
- android--菜单
- CrossWalk项目main_delegate类的问题
- Hardcoded string "xxxxxxxxxxxx", should use @string resource
- android ui 遇到的问题汇总
- android 点击获取验证码显示倒计时并不可用
- Android(安卓)Studio使用技巧
- Android(安卓)应用第一次运行时,引导页面的设置方法(只让程序Aciti