**
类似底部导航栏的菜单,点击图标,文字和图标都变颜色,Fragment切换界面详解
**
先看效果图
以下是全部完整代码,如果有问题欢迎留言
- 图标和文字布局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>
- 设置点击后变化颜色的逻辑代码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