android tab样式
时间: 2023-09-24 19:09:42 浏览: 176
Android Tab 可以使用以下两种样式:
1. 顶部 Tab 样式:
在布局文件中使用 TabLayout 控件,然后在代码中为 TabLayout 添加 Tab,可以设置 Tab 的标题、图标等属性。可以使用 setCustomView() 方法自定义 Tab 的布局。
示例代码:
```xml
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
```
```java
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
```
效果图:

2. 底部 Tab 样式:
可以使用 BottomNavigationView 控件实现底部 Tab 样式,类似于微信、QQ 等应用的底部导航栏。
示例代码:
```xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_menu" />
```
```java
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.menu_home:
// 切换到首页
break;
case R.id.menu_message:
// 切换到消息页面
break;
case R.id.menu_mine:
// 切换到个人中心页面
break;
}
return true;
});
```
效果图:

阅读全文
相关推荐


















