【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;
    }
}
复制代码





本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/4286672.html,如需转载请自行联系原作者
目录
相关文章
|
15天前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
15 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
16天前
|
消息中间件 安全 数据处理
Android为什么不能在子线程更新UI
Android为什么不能在子线程更新UI
23 0
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
41 1
|
3月前
|
开发工具 Android开发 开发者
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
31 4
|
3月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
31 2
|
3月前
|
Android开发 容器
Android UI设计: 什么是View和ViewGroup?
Android UI设计: 什么是View和ViewGroup?
36 0
|
3月前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
92 5
|
5天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。
|
11天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4