fragment 切换,导航栏是图标加文字
来源:互联网 发布:义乌淘宝美工学校 编辑:程序博客网 时间:2024/06/05 03:09
fragment 切换,导航栏是图标加文字
activity_main.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> //viewpager切换 <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <View android:id="@+id/v_line" android:layout_width="match_parent" android:layout_height="2px" android:background="@color/white"/> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="40dp" app:tabBackground="@color/white" app:tabIndicatorHeight="0dp" app:tabSelectedTextColor="@color/colorPrimary" app:tabTextColor="@color/black"/></LinearLayout>
java代码
tablayout_view
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_icon" android:layout_width="24dp" android:layout_height="24dp" /> <TextView android:id="@+id/tv_name" android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp"/></LinearLayout>
fragment切换使用的adapter
public class ViewPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragmentList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.fragmentList.clear(); this.fragmentList.addAll(fragmentList); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); }}
MainActivity 使用时的代码
public class MainActivity extends FragmentActivity { ViewPager viewPager; TabLayout tabLayout; TabLayout.Tab tab1; TabLayout.Tab tab2; TabLayout.Tab tab3; private int num=0; private List<Fragment> fragmentList = new ArrayList<>(); private int[] mTabCheck = new int[]{R.drawable.ic_book, R.drawable.ic_recommed, R.drawable.ic_user}; private int[] mTabUnCheck = new int[]{R.drawable.ic_un_book, R.drawable.ic_un_recommed, R.drawable.ic_un_user}; private String[] titles = new String[]{"书架","推荐","我的"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.view_pager); tabLayout = (TabLayout) findViewById(R.id.tab_layout); //切换用到的fragment fragmentList.add(new BookFragment()); fragmentList.add(new RecommedFragment()); fragmentList.add(new UserFragment()); ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList); viewPager.setAdapter(viewPagerAdapter); viewPager.setCurrentItem(0); tabLayout.setupWithViewPager(viewPager); tabLayout.setTabMode(TabLayout.MODE_FIXED);//设置TabLayout的模式 //设置导航栏 tab1 = tabLayout.getTabAt(0); tab1.setCustomView(getTabView(0)); tab2 = tabLayout.getTabAt(1); tab2.setCustomView(getUnTabView(1)); tab3 = tabLayout.getTabAt(2); tab3.setCustomView(getUnTabView(2)); //切换监听事件 tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { Select(tab); } @Override public void onTabUnselected(TabLayout.Tab tab) { UnSelect(tab); } @Override public void onTabReselected(TabLayout.Tab tab) { } });//设置TabLayout的选中监听 } public void Select(TabLayout.Tab tab) { View v = tab.getCustomView(); v.findViewById(R.id.iv_icon).setBackgroundResource(mTabCheck[tab.getPosition()]); TextView tv = (TextView) v.findViewById(R.id.tv_name); tv.setTextColor(getResources().getColor(R.color.menu_select)); } public void UnSelect(TabLayout.Tab tab) { View v = tab.getCustomView(); v.findViewById(R.id.iv_icon).setBackgroundResource(mTabUnCheck[tab.getPosition()]); TextView tv = (TextView) v.findViewById(R.id.tv_name); tv.setTextColor(getResources().getColor(R.color.menu_unselect)); } public View getTabView(int position) { View view = LayoutInflater.from(this).inflate(R.layout.tablayout_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon); TextView textView = (TextView) view.findViewById(R.id.tv_name); imageView.setBackgroundResource(mTabCheck[position]); textView.setText(titles[position]); textView.setTextColor(getResources().getColor(R.color.menu_select)); return view; } public View getUnTabView(int position) { View view = LayoutInflater.from(this).inflate(R.layout.tablayout_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon); TextView textView = (TextView) view.findViewById(R.id.tv_name); imageView.setBackgroundResource(mTabUnCheck[position]); textView.setText(titles[position]); textView.setTextColor(getResources().getColor(R.color.menu_unselect)); return view; }}
阅读全文
0 0
- fragment 切换,导航栏是图标加文字
- TabLayout+ViewPager+Fragment实现带图标和文字的底部导航栏
- 导航抽屉图标+文字
- Swift - 修改导航栏“返回”按钮文字,图标
- Swift - 修改导航栏“返回”按钮文字,图标
- Swift - 修改导航栏“返回”按钮文字,图标
- 极致精简的fragment实现导航栏切换demo
- 安卓底部导航栏点击变色切换不同Fragment
- ViewPager+Fragment滑动切换页面(RadioButton底部导航栏)
- Fragment 实现底部导航栏的简单切换
- Andriod底部导航栏图片和文字的切换
- Fragment切换。radiobutton加fragment切换(附件源码下载)
- 针对下方是导航条,上方是Fragment; 包括Fragment再嵌套Fragment进行切换(类似QQ消息模块中消息,电话)的UI框架
- 在文字前加图标技巧
- JS中文字前面加图标
- fragment的简单使用,点击底下导航栏切换activity的fragment。
- Fragment 搭建框架 实现 导航 切换
- fragment+Radiobutton实现顶部导航切换
- 8.10抽象类与接口
- CentOS下安装Python3后BeautifulSoup 版本不兼容问题解决方法
- [编程题] Fibonacci数列
- ubuntu server 16.04安装与网络配置
- android studio 代码模板
- fragment 切换,导航栏是图标加文字
- MySQL 调优/优化的 100 个建议
- 方法中定义一个内部类对象时,如何在内部类对象中使用调用该方法的当前对象的其他方法
- 应届毕业生如何通过学习Linux系统选择一份高薪职业
- 安卓工作室 android studio 谷歌账号 登录
- 《ECMAScript 6入门》笔记3
- (4)字符集、校对规则
- v$asm_diskgroup状态connected与mounted的区别
- 系统恢复