Android底部菜单-FragmentTabHost实现
来源:互联网 发布:手机视频录播软件 编辑:程序博客网 时间:2024/05/22 06:58
Android 底部菜单(FragmentTabHost实现)
该部分源码均来自菜鸟网菜鸟商城(http://www.cniao5.com/course/10073)
MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.shangcheng.MainActivity"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@android:color/darker_gray"></FrameLayout> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"></FrameLayout> </android.support.v4.app.FragmentTabHost></LinearLayout>
MainActivity.java文件代码如下:
public class MainActivity extends AppCompatActivity { private LayoutInflater mInflater; private FragmentTabHost mTabhost; private List<Tab> mTabs = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mInflater = LayoutInflater.from(this); initTabs(); } private void initTabs() { Tab tab_home = new Tab(R.string.home, R.drawable.selector_icon_home, HomeFragment.class); Tab tab_mine = new Tab(R.string.mine, R.drawable.selector_icon_mine, MineFragment.class); mTabs.add(tab_home); mTabs.add(tab_mine); mTabhost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); mTabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); for (Tab tab : mTabs) { TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle())); tabSpec.setIndicator(buildIndicator(tab)); mTabhost.addTab(tabSpec, tab.getFragment(), null); } mTabhost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); mTabhost.setCurrentTab(0); } private View buildIndicator(Tab tab) { View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView img = view.findViewById(R.id.icon_tab); TextView text = view.findViewById(R.id.text_indicator); img.setBackgroundResource(tab.getIcon()); text.setText(tab.getTitle()); return view; }}
Tab对象源码如下:
public class Tab { private int title; private int icon; private Class fragment; public Tab(int title, int icon, Class fragment) { this.title = title; this.icon = icon; this.fragment = fragment; } // getter、setter方法这里省略
selector_icon_home源码如下:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_home" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_home_press" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_home_press" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_home_press" /> <!-- Pressed --> <item android:state_selected="true" android:state_pressed="true" android:drawable="@mipmap/icon_home_press" /> <item android:state_pressed="true" android:drawable="@mipmap/icon_home_press" /></selector>
视图tab_indicator源码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" android:paddingBottom="3dp" android:paddingTop="3dp"> <ImageView android:id="@+id/icon_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:textColor="@color/selector_tab_text" /></LinearLayout>
源码selector_tab_text如下:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color="#eb4f38" /> <item android:state_active="true" android:color="#eb4f38"/> <item android:state_selected="false" android:color="#a9b7b7" /> <item android:state_active="false" android:color="#a9b7b7"/></selector>
效果图如下:
完毕!
阅读全文
0 0
- Android底部菜单-FragmentTabHost实现
- 【Android】FragmentTabHost实现底部Tab菜单选项
- 【Android UI】FragmentTabHost 实现底部菜单
- 用 FragmentTabHost 实现底部菜单
- FragmentTabHost实现底部导航菜单
- 用FragmentTabHost 实现底部菜单
- Android控件使用之FragmentTabHost实现底部菜单
- fragmentTabhost+Fragment底部菜单实现方法
- 用FragmentTabHost加Fragment实现底部菜单
- FragmentTabHost+Fragment实现底部菜单的切换
- 底部菜单实现(三)fragmentTabHost和fragment
- 用 FragmentTabHost 实现仿淘宝底部菜单
- Android学习之--底部菜单FragmentTabHost使用
- android 底部菜单写法之FragmentTabHost
- 【Android】底部菜单栏--Fragment+FragmentTabHost实现
- #Android笔记#fragment+fragmentTabHost实现底部菜单栏
- Android FragmentTabHost实现底部tab切换
- Android中使用FragmentTabHost实现底部导航
- Ubuntu下中文输入法的解决办法
- 【Leetcode】【python】Swap Nodes in Pairs
- Spring-AOP @AspectJ切点函数之@within()和@target
- C#--继承的关键字
- Python之 __slots__ 的用法
- Android底部菜单-FragmentTabHost实现
- nohup介绍
- ZeroBrane Studio 设置
- 【LeetCode】Partition List 链表划分
- Python之 @property 用法
- 关于jquery里面prop和attr的带示例详细对比,深度理解
- 文件系统学习1 四巨头
- 【LeetCode】Reverse Linked List
- phpstorm命令行运行console