android便于维护的下面选项卡状态切换
来源:互联网 发布:jquery rotate.js cdn 编辑:程序博客网 时间:2024/06/01 08:42
效果图
布局
<LinearLayout android:id="@+id/main_bottome_switcher_container" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="30dp" android:src="@drawable/home" /> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:layout_gravity="bottom" android:gravity="center" android:text="首页" android:textColor="@color/main_bottom_tv_color" /> </FrameLayout> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="30dp" android:src="@drawable/order" /> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:layout_gravity="bottom" android:gravity="center" android:text="订单" android:textColor="@color/main_bottom_tv_color" /> </FrameLayout> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="30dp" android:src="@drawable/me" /> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:layout_gravity="bottom" android:gravity="center" android:text="个人" android:textColor="@color/main_bottom_tv_color" /> </FrameLayout> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="30dp" android:src="@drawable/more" /> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:layout_gravity="bottom" android:gravity="center" android:text="更多" android:textColor="@color/main_bottom_tv_color" /> </FrameLayout> </LinearLayout>
代码
找到id后 onCreate中写这三个方法
//点击底部4个选项卡 目的切换顶部真布局执行的fragment准备4geFragment initFragment(); initClick(); //默认选中索引位置为0的页面 onClick(mainBottomeSwitcherContainer.getChildAt(0));方法的具体实现
private void initClick() { int childCount = mainBottomeSwitcherContainer.getChildCount(); for (int i = 0; i < childCount; i++) { FrameLayout frameLayout = (FrameLayout) mainBottomeSwitcherContainer.getChildAt(i); frameLayout.setOnClickListener(this); } } private void initFragment() { fragments = new ArrayList<>(); fragments.add(new HomeFragment()); fragments.add(new OrderFragment()); fragments.add(new UserFragment()); fragments.add(new MoreFragment()); } @Override public void onClick(View v) { //提供点中所在线性布局容器中的索引位置 int indexOfChild = mainBottomeSwitcherContainer.indexOfChild(v); //切换选中FrameLayout内部孩子节点的颜色 changeUI(indexOfChild); // 切换Fragment(首页 订单 用户 更多) changeFragment(indexOfChild); } private void changeFragment(int indexOfChild) { fragments.size(); getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container,fragments.get(indexOfChild),indexOfChild+"").commit(); } /** * 切换选中FrameLayout内部孩子节点的颜色 * * @param indexOfChild */ private void changeUI(int indexOfChild) { int childCount = mainBottomeSwitcherContainer.getChildCount(); for (int i = 0; i < childCount; i++) { //获取每一个孩子节点,如果选中索引和孩子节点索引一致,则设置控件为不可用,不可用对应蓝色效果 FrameLayout frameLayout = (FrameLayout)mainBottomeSwitcherContainer.getChildAt(i); if (i == indexOfChild) { //找到了选中的对象 以及孩子都需要要变成蓝色 setEnable(frameLayout, false); } else { // 没找到的 不需要变色 setEnable(frameLayout, true); } } } /** * @param view * @param b */ private void setEnable(View view, boolean b) { //父布局操作 view.setEnabled(b); //view 是否能变成viewGroup判断 if (view instanceof ViewGroup) { int childCount = ((ViewGroup) view).getChildCount(); for (int i = 0; i < childCount; i++) { View viewchild = ((ViewGroup) view).getChildAt(i);// viewchild.setEnabled(b); //更高级的写法 递归调用 这样写的好处是 如果除了这两个子类还有子类的话 还能继续往下设置 setEnable(viewchild, b); } } }对了 这里的selector 用的是state_enable属性
home.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/home_normal" android:state_enabled="true"/> <item android:drawable="@drawable/home_disabled" android:state_enabled="false"/></selector>main_bottom_tv_color
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#000" android:state_enabled="true"/> <item android:color="#28b2ed" android:state_enabled="false"/></selector>
0 0
- android便于维护的下面选项卡状态切换
- Android横竖屏切换时,状态和信息的维护
- 选项卡选中状态的实现及其多个选项时按钮的状态切换((每个选项其实都是按钮))
- android开发(8) 选项卡的切换
- android开发(8) 选项卡的切换
- android开发(8) 选项卡的切换
- Android布局方法-(便于后期维护)
- Android按钮状态的切换
- 简单的选项卡切换
- 便于更佳理解Socket编程,引入下面的讲解
- Android TabHost 选项卡 滑动activity进行切换选项卡
- Android ViewPager Fragment 选项卡切换
- 物料的维护状态
- Android ToggleButton:状态切换的Button
- Android基础知识---ImageView的状态切换
- Android ToggleButton:状态切换的Button
- 各种功能的选项卡切换
- 禁止tabhost选项卡的切换
- ChucK初步(4)
- 如果你不是每天被人拒绝,那就说明你的人生目标不够远大。(If you aren't getting rejected on a daily basis, your goals aren't ambit
- suse linux 文件目录备份和恢复
- git最常用命令
- Leetcode 115 - Distinct Subsequences(dp)
- android便于维护的下面选项卡状态切换
- Java EE开发技术
- bootstrap-table删除指定行注意事项
- Python与人工神经网络(1)——开一个新坑
- Android 4.4从图库选择图片并裁剪,由于系统版本不同导致Uri的bug的几种解决方案
- 【HTML & CSS & JavaScript】JS与Flash的相互调用
- hdu 2047 阿牛的EOF牛肉串(递推)
- 两个对应不同类的list如果有相同的id,差集去重
- VTK修炼之道56:图形基本操作进阶_表面重建技术(三维点云曲面重建)