Android仿微信滑动切换最终实现效果:大体思路: 主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标;2. 底部导航栏的设置方法类似于TabLayout的关联,View
大体思路:
主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标;
2. 底部导航栏的设置方法类似于TabLayout的关联,View需要创建关联方法,用来关联VIewPager;
3. 通过关联方法获取ViewPager实例后,根据ViewPager页面数创建底部导航栏的图标按钮;
代码实现:
新建第一个自定义View, 图标 + 文字 的底部按钮;
public class TabView extends LinearLayout { BotBean mBean; private TextView title; private ImageView iconImage; public TabView(Context context, BotBean bean) { super(context); this.mBean = bean; initView(); } public void initView() { setOrientation(VERTICAL); setGravity(Gravity.CENTER); //添加小图标 iconImage = new ImageView(getContext()); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT); iconImage.setLayoutParams(layoutParams); iconImage.setImageResource(mBean.getUncheckedId()); Drawable drawable = getContext().getResources().getDrawable(mBean.getUncheckedId()); Drawable wrapDrawable = DrawableCompat.wrap(drawable); DrawableCompat.setTintList(wrapDrawable, ColorStateList.valueOf(Color.BLACK)); iconImage.setImageDrawable(wrapDrawable); addView(iconImage); //标题 title = new TextView(getContext()); LinearLayout.LayoutParams titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); title.setLayoutParams(titleParams); title.setText(mBean.getContent()); addView(title); } //判断选择状态,改变图标 //供外部调用 public void setSelected(boolean isSelected) { if (mBean == null) { return; } if (isSelected) { if (iconImage != null) { //使用颜色过滤器,改变选中时的颜色 Drawable drawable = getContext().getResources().getDrawable(mBean.getUncheckedId()); Drawable wrapDrawable = DrawableCompat.wrap(drawable); DrawableCompat.setTintList(wrapDrawable, ColorStateList.valueOf(Color.GREEN)); iconImage.setImageDrawable(wrapDrawable); title.setTextColor(Color.GREEN); } } else { if (title != null) { // iconImage.setImageResource(mBean.getUncheckedId()); Drawable drawable = getContext().getResources().getDrawable(mBean.getUncheckedId()); Drawable wrapDrawable = DrawableCompat.wrap(drawable); DrawableCompat.setTintList(wrapDrawable, ColorStateList.valueOf(Color.BLACK)); iconImage.setImageDrawable(wrapDrawable); title.setTextColor(Color.GRAY); } } } }
--结束END--
本文标题: Android实现简单底部导航栏 Android仿微信滑动切换效果
本文链接: https://www.lsjlt.com/news/221578.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0