android 底部菜单写法之FragmentTabHost
来源:互联网 发布:java的作用 编辑:程序博客网 时间:2024/05/29 04:20
android底部菜单写法很多种类,今天用的是FragmentTabHost:代码很简单:
Mainacitivity中代码:BaseActivity继承的FragmentActivity
@ContentView(R.layout.activity_main)public class MainActivity extends BaseActivity { private String[] names = {"书架", "书城", "我的"}; private String[] tags = {"bookshelf", "bookshop", "aboutme"}; private Class[] fragmentClass = {BookshelfFragment.class, BookshopFragment.class, AboutmeFragment.class}; private int[] images = {R.drawable.tab_bookshelf_selector, R.drawable.tab_bookshop_selector, R.drawable.tab_aboutme_selector,}; @ViewInject(android.R.id.tabhost) private FragmentTabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); x.view().inject(this); tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); tabHost.getTabWidget().setDividerDrawable(null); for (int i = 0; i < images.length; i++) { TabHost.TabSpec tabSpec = tabHost.newTabSpec(tags[i]).setIndicator(getImageView(i)); tabHost.addTab(tabSpec, fragmentClass[i], null); tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.material_grey_100); } } private View getImageView(int index) { View view = View.inflate(this, R.layout.item_tab, null); ImageView iv = (ImageView) view.findViewById(R.id.iv_icon); TextView tv = (TextView) view.findViewById(R.id.tv_name); iv.setImageResource(images[index]); tv.setText(names[index]); return view; }}
<?xml version="1.0" encoding="utf-8"?><android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"/> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> </RelativeLayout></android.support.v4.app.FragmentTabHost>
布局文件item_tab代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:padding="3dp"> <ImageView android:id="@+id/iv_icon" android:layout_width="30dp" android:layout_height="30dp" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/tab_textcolor_selector"/></LinearLayout>
选择器tab_textcolor_selector代码:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/orange" android:state_selected="true"/> <item android:color="@color/black"/></selector>
最终效果:
阅读全文
0 0
- android 底部菜单写法之FragmentTabHost
- Android学习之--底部菜单FragmentTabHost使用
- Android底部菜单-FragmentTabHost实现
- Android控件使用之FragmentTabHost实现底部菜单
- 【Android】FragmentTabHost实现底部Tab菜单选项
- 【Android UI】FragmentTabHost 实现底部菜单
- Android项目开发实战之使用Fragment和FragmentTabHost搭建底部菜单(一)
- 用 FragmentTabHost 实现底部菜单
- FragmentTabHost实现底部导航菜单
- 用FragmentTabHost 实现底部菜单
- 利用FragmentTabHost完成底部菜单
- BottomBar之Android底部菜单
- fragmentTabhost+Fragment底部菜单实现方法
- 用FragmentTabHost加Fragment实现底部菜单
- FragmentTabHost+Fragment实现底部菜单的切换
- 底部菜单实现(三)fragmentTabHost和fragment
- 用 FragmentTabHost 实现仿淘宝底部菜单
- Android之底部菜单 BottomNavigationBar的使用
- python3 日志记录模块 logging
- PHP 工具
- vivo杭州研发中心投入使用 拍照和人工智能是重点
- 星河互联CEO傅淼:从商业智能到智能商业 | AI这个6岁偏科神童如何做商业决策? | CCF-GAIR 2017
- 寻一城美景,觅非凡之美,Xplay6与重庆的小故事
- android 底部菜单写法之FragmentTabHost
- “你是我的眼”,微软计算机视觉 APP 让盲人用手机看世界
- 比科幻更现实,超级高铁公司Hyperloop One完成首次全真空测试
- openwrt firewall
- 海康威视涉嫌抄袭,拥有 1200 项专利的安防龙头怎么想的?
- 运行程序选择总是重启tomcat后,如何修改设置。
- 亲见智会打造办公神器,黑科技造福空中飞人
- Populating Next Right Pointers in Each Node I, II
- python3 subprocess模块使用