android底部导航栏点击时突出显示
时间: 2023-08-15 19:03:21 浏览: 52
在Android底部导航栏中,当用户点击选项卡时,可以通过设置选项卡的选中状态来实现突出显示的效果。可以按照以下步骤来实现:
1. 在布局文件中添加底部导航栏:使用`BottomNavigationView`组件来创建底部导航栏,并添加多个选项卡。例如:
```xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_menu" />
```
2. 在菜单文件中定义选项卡:在`res/menu`目录下创建一个菜单文件,并添加多个菜单项,每个菜单项对应一个选项卡。例如:
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_home"
android:title="Home"
android:icon="@drawable/ic_home" />
<item
android:id="@+id/menu_dashboard"
android:title="Dashboard"
android:icon="@drawable/ic_dashboard" />
<item
android:id="@+id/menu_notifications"
android:title="Notifications"
android:icon="@drawable/ic_notifications" />
</menu>
```
3. 在Activity中处理菜单项点击事件:在每个Activity中,实现`OnNavigationItemSelectedListener`接口,并在`onNavigationItemSelected()`方法中处理菜单项点击事件。在处理完点击事件后,调用选项卡的`setChecked()`方法来设置选中状态。例如:
```java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private BottomNavigationView mBottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNavigationView = findViewById(R.id.bottom_navigation);
mBottomNavigationView.setOnNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
// do something
mBottomNavigationView.getMenu().getItem(0).setChecked(true);
return true;
case R.id.menu_dashboard:
// do something
mBottomNavigationView.getMenu().getItem(1).setChecked(true);
return true;
case R.id.menu_notifications:
// do something
mBottomNavigationView.getMenu().getItem(2).setChecked(true);
return true;
default:
return false;
}
}
}
```
通过这种方式,在用户点击选项卡时,被点击的选项卡会突出显示,其他选项卡则会恢复正常状态。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)