Android底部导航栏切换界面,点击选项时文字和图标改变颜色

本文详细介绍了如何在Android中实现底部导航栏点击时,图标和文字颜色的变化,并配合Fragment进行界面切换。通过示例代码和布局文件,展示了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

类似底部导航栏的菜单,点击图标,文字和图标都变颜色,Fragment切换界面详解

**
先看效果图
在这里插入图片描述
以下是全部完整代码,如果有问题欢迎留言

  1. 图标和文字布局color_change.xml
 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:background="#60c49e">
        <ImageView
            android:id="@+id/bottom_icon"
            android:layout_width="40dp"
            android:layout_height="40dp"/>
        <TextView
            android:id="@+id/bottom_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#ffffff"/>
    </LinearLayout>
  1. 设置点击后变化颜色的逻辑代码BottomLayout.java
public class BottomLayout extends LinearLayout{
    private int normalIcon;
    private int focusedIcon;
    private boolean isFocused = false;
    private ImageView ivIcon;
    private TextView tvText;
    public BottomLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.color_change,this);
        ivIcon = findViewById(R.id.bottom_icon);
        tvText = findViewById(R.id.bottom_text);
    }
    public void setNormalIcon(int normalIcon){
        this.normalIcon = normalIcon;
        ivIcon.setImageResource(normalIcon);

    }
    public void setFocus
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值