Fragment实现底部菜单栏

来源:互联网 发布:mysql 字符函数 编辑:程序博客网 时间:2024/05/22 07:16

【Android UI设计与开发】5.底部菜单栏(二)使用Fragment实现底部菜单栏

既然 Fragment 取代了TabActivity,当然 TabActivity 的能实现的菜单栏,Fragment 当然也能实现。主要其实就是通过菜单栏的点击事件切换 Fragment 的显示和隐藏。

来看看栗子吧:

1.效果图来了:

 

2.代码具体实现

2.1 自定义底部菜单栏实现方式

(1)对应的 Fragment 编辑代码和布局实现在前面的 Fragment介绍和简单实现  中已经有提及,代码中没复杂的地方,此处略过,具体可看实例代码。

(2)菜单栏实现,这里使用代码实现的,其实也可以用布局文件实现,代码如下:

ViewIndicator

(3)最后就是主界面代码,切换 Fragment 的显示和隐藏以及菜单栏的选中状态

复制代码
package com.yanis.yc_ui_fragment_menu;import com.yanis.yc_ui_fragment_menu.ViewIndicator.OnIndicateListener;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.view.View;public class MainActivity extends FragmentActivity {     public static Fragment[] mFragments;           @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                setFragmentIndicator(0);      }         /**      * 初始化fragment      */      private void setFragmentIndicator(int whichIsDefault) {         //实例化 Fragment 集合         mFragments = new Fragment[5];           mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_home);           mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_category);           mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_down);           mFragments[3] = getSupportFragmentManager().findFragmentById(R.id.fragment_user);           mFragments[4] = getSupportFragmentManager().findFragmentById(R.id.fragment_setting);           //显示默认的Fragment         getSupportFragmentManager().beginTransaction().hide(mFragments[0])                   .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]).show(mFragments[whichIsDefault]).commit();          //绑定自定义的菜单栏组件        ViewIndicator mIndicator = (ViewIndicator) findViewById(R.id.indicator);          ViewIndicator.setIndicator(whichIsDefault);          mIndicator.setOnIndicateListener(new OnIndicateListener() {              @Override              public void onIndicate(View v, int which) {                  //显示指定的Fragment                  getSupportFragmentManager().beginTransaction()                    .hide(mFragments[0]).hide(mFragments[1])                    .hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]).show(mFragments[which]).commit();             }          });      }  }
复制代码

 

源代码地址:https://github.com/YeXiaoChao/Yc_ui_fragment_menu

 


 

 

2.2 使用 Fragment+FragmentTabHost 来实现底部菜单栏方式

效果是一样的,只是在上面的基础上使用 FragmentTabHost 来实现底部菜单栏,直接通过 FragmentTabHost 来切换 Fragment 的显示 ,而不是自定义的布局。

(1)修改主布局代码,加入了FragmentTabHost 组件

复制代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <FrameLayout        android:id="@+id/realtabcontent"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1" />    <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="fill_parent"        android:layout_height="wrap_content"         android:background="@drawable/main_tab_item_bg">        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />                </android.support.v4.app.FragmentTabHost></LinearLayout>
复制代码

(2)单独为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" >    <ImageView        android:id="@+id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:focusable="false"        android:padding="3dp"         android:src="@drawable/main_tab_item_home">    </ImageView>    <TextView        android:id="@+id/textview"               android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:textSize="12sp"        android:textColor="#ffffff">    </TextView></LinearLayout>
复制代码

(3) fragment布局界面和之前一样,就不再赘述

(4) Tab选项的自定义按钮中图片资源文件,列出其中一个按钮,指定了按钮的选中状态和不选中状态不同的图片显示

复制代码
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/main_tab_item_home_focus" android:state_selected="true"/>    <item android:drawable="@drawable/main_tab_item_home_normal"/></selector>
复制代码

(5) Tab选项按钮背景资源文件,指定了点击的效果

复制代码
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- pressed -->    <item android:drawable="@drawable/main_tab_item_bg_focus" android:state_enabled="true" android:state_pressed="true"/>    <!-- focused -->    <item android:drawable="@drawable/main_tab_item_bg_focus" android:state_enabled="true" android:state_focused="true"/>    <!-- normal -->    <item android:drawable="@drawable/main_tab_item_bg_normal" android:state_enabled="true"/></selector>
复制代码

(6) 最后就是主界面代码的改变

复制代码
package com.yanis.yc_ui_fragment_tabhost;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TabHost.TabSpec;import android.widget.TextView;/** *  * @author yechao * @功能说明 自定义TabHost * */public class MainActivity extends FragmentActivity {    // 定义FragmentTabHost对象    private FragmentTabHost mTabHost;    // 定义一个布局    private LayoutInflater layoutInflater;    // 定义数组来存放Fragment界面    private Class fragmentArray[] = { FragmentHome.class,            FragmentCategory.class, FragmentDown.class, FragmentUser.class,            FragmentSetting.class };    // 定义数组来存放按钮图片    private int mImageViewArray[] = { R.drawable.main_tab_item_home,            R.drawable.main_tab_item_category, R.drawable.main_tab_item_down,            R.drawable.main_tab_item_user, R.drawable.main_tab_item_setting };    // Tab选项卡的文字    private String mTextviewArray[] = { "主页", "分类", "下载", "我的", "设置" };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    /**     * 初始化组件     */    private void initView() {        // 实例化布局对象        layoutInflater = LayoutInflater.from(this);        // 实例化TabHost对象,得到TabHost        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);        // 得到fragment的个数        int count = fragmentArray.length;        for (int i = 0; i < count; i++) {            // 为每一个Tab按钮设置图标、文字和内容            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])                    .setIndicator(getTabItemView(i));            // 将Tab按钮添加进Tab选项卡中            mTabHost.addTab(tabSpec, fragmentArray[i], null);            // 设置Tab按钮的背景            mTabHost.getTabWidget().getChildAt(i)                    .setBackgroundResource(R.drawable.main_tab_item_bg);        }    }    /**     * 给Tab按钮设置图标和文字     */    private View getTabItemView(int index) {        View view = layoutInflater.inflate(R.layout.tab_item_view, null);        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);        imageView.setImageResource(mImageViewArray[index]);        TextView textView = (TextView) view.findViewById(R.id.textview);        textView.setText(mTextviewArray[index]);        return view;    }}
复制代码

 

源代码地址:https://github.com/YeXiaoChao/Yc_ui_fragment_tabhost

原创粉丝点击