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>

效果图如下:
这里写图片描述

完毕!