android中 Fragment+FragmentTabHost

记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost

安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航)

activity_main.xml(主布局文件)

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6
 7     <!-- 存放主要页面内容 -->
 8
 9     <FrameLayout
10         android:id="@+id/maincontent"
11         android:layout_width="fill_parent"
12         android:layout_height="0dp"
13         android:layout_weight="1" >
14     </FrameLayout>
15
16     <!-- 底层菜单 -->
17
18     <android.support.v4.app.FragmentTabHost
19         android:id="@android:id/tabhost"
20         android:layout_width="fill_parent"
21         android:layout_height="wrap_content"
22         android:background="@drawable/maintab_toolbar_bg" >
23
24         <FrameLayout
25             android:id="@android:id/tabcontent"
26             android:layout_width="0dp"
27             android:layout_height="0dp"
28             android:layout_weight="0" >
29         </FrameLayout>
30     </android.support.v4.app.FragmentTabHost>
31
32 </LinearLayout>

fragment.xml(由于只有文字不同,这里只给出一个)

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5  >
 6
 7
 8     <TextView
 9         android:id="@+id/text"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_centerInParent="true"
13         android:text="我是第一个Fragment"
14         android:textSize="20dp"
15         />
16
17
18 </RelativeLayout>

tabcontent.xml(具体底部菜单详细布局)

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:gravity="center_horizontal"
 6     android:orientation="vertical" >
 7
 8     <ImageView
 9         android:id="@+id/image"
10         android:layout_height="wrap_content"
11         android:layout_width="wrap_content"
12         />
13     <TextView
14         android:id="@+id/text"
15         android:padding="2dp"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:textColor="@android:color/white"
19         />
20
21
22 </LinearLayout>

bt_selector.xml(底部菜单点击背景)

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3
4     <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"></item>
5     <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"></item>
6
7 </selector>

bt_home_selector.xml(底部菜单按钮效果)

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3
4     <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"></item>
5     <item android:drawable="@drawable/icon_home_nor"></item>
6
7 </selector>

FragmentPage1-FragmentPage5.java

 1 package com.example.newtabhosttest;
 2
 3 import android.os.Bundle;
 4 import android.support.annotation.Nullable;
 5 import android.support.v4.app.Fragment;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9
10 public class FragmentPage1 extends Fragment{
11         @Override
12         public View onCreateView(LayoutInflater inflater,
13                 @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
14             return inflater.inflate(R.layout.fragment1, null);
15         }
16
17 }

MainActivity.java(主代码)

 1 package com.example.newtabhosttest;
 2
 3 import android.os.Bundle;
 4 import android.support.v4.app.FragmentActivity;
 5 import android.support.v4.app.FragmentTabHost;
 6 import android.view.View;
 7 import android.widget.ImageView;
 8 import android.widget.TabHost.TabSpec;
 9 import android.widget.TextView;
10
11 public class MainActivity extends FragmentActivity {
12
13     private FragmentTabHost fragmentTabHost;
14     private String texts[] = { "首页", "消息", "好友", "广场", "更多" };
15     private int imageButton[] = { R.drawable.bt_home_selector,
16             R.drawable.bt_message_selector, R.drawable.bt_selfinfo_selector,R.drawable.bt_square_selector ,R.drawable.bt_more_selector};
17      private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};
18
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23
24         // 实例化tabhost
25         fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
26         fragmentTabHost.setup(this, getSupportFragmentManager(),
27                 R.id.maincontent);
28
29         for (int i = 0; i < texts.length; i++) {
30             TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
31
32             fragmentTabHost.addTab(spec, fragmentArray[i], null);
33
34             //设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常)
35             fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
36         }
37
38     }
39
40     private View getView(int i) {
41         //取得布局实例
42         View view=View.inflate(MainActivity.this, R.layout.tabcontent, null);
43
44         //取得布局对象
45         ImageView imageView=(ImageView) view.findViewById(R.id.image);
46         TextView textView=(TextView) view.findViewById(R.id.text);
47
48         //设置图标
49         imageView.setImageResource(imageButton[i]);
50         //设置标题
51         textView.setText(texts[i]);
52         return view;
53     }
54
55 }

时间: 2024-08-06 11:55:29

android中 Fragment+FragmentTabHost的相关文章

android中fragment和activity之间相互通信

在用到fragment的时候,老是会遇到一个问题,就是fragment与activity之间的通信.下面就来记录一下activity和fragment之间 通过实现接口来互相通信的方法. 1. activity 向fragment发出通信,就这么写: private OnMainListener mainListener; // 绑定接口 @Override public void onAttachFragment(Fragmentfragment) { try { mainListener =

Android中Fragment和ViewPager那点事儿

在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragment与Activity之间的交互(两种实现方式)>中我们介绍了ViewPager以及Fragment各自的使用场景以及不同的实现方式. 那如果将他们两结合起来,会不会擦出点火花呢,答案是肯定的.之前在介绍ViewPager时,我们实现了多个ImageView的切换,并配合更新导航原点的状态.那我们现在

Android中Fragment与Activity之间的交互(两种实现方式)

(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如何创建Fragment混合布局做了详细的分析,今天就来详细说道说道Fragment与宿主Activity之间是如何实现数据交互的. 我们可以这样理解,宿主Activity中的Fragment之间要实现信息交互,就必须通过宿主Activity,Fragment之间是不可能直接实现信息交互的. Fragment与

Android中Fragment和Activity之间的互操作代码例子

摘要 本文介绍了Android中一个Activity中有多个Fragment的情况下,Fragment之间如何通过Activity进行互操作. 源代码 源代码地址为:http://download.csdn.net/detail/logicteamleader/8931199 源代码使用ADT编写,ADT版本为2014,Android版本为android-22. 技术要点 1.在Activity中的多个Fragment之间要互操作,一定要通过此Activity,不能直接通信: 2.在Activi

Android中fragment之间和Activity的传值、切换

功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragment,用以显示不同的内容. 所用的知识点:当单击发送消息按钮时: 1.从MainActivity中把EditText中的值传到fragment中. 2.fragment如何动态的显示在MainActivity中. 针对第一个问题:在sendButton单击事件中: private OnClickLis

ANDROID中FRAGMENT的两种创建方式

fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认为fragment是activity的一个模块零件,它有自己的生命周期,接收它自己的输入事件,并且可以在Activity运行时添加或者删除. 两个概念:Fragment.宿主 fragment的生命周期直接受其宿主activity的生命周期的影响.例如,一旦activity被暂停,它里面所有的fra

Android中Fragment的分屏显示处理横竖屏显示

演示效果如下: 另外在竖屏的时候是这样的效果: 布局文件如下: 可以看出有两个资源文件,一个是处理横屏一个是竖屏 第一个: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent&qu

Android中Fragment的使用

Fragment可能是我心中一直以来的执念,由于Android开发并没有像一般流程一样系统的学习,而是直接在公司项目中改bug开始的.当时正是Fragment被提出来的时候,那时把全部精力放到了梳理代码业务逻辑上,错过了Fragment首班车,而这一等就到现在. Android发布的前两个版本只适配小尺寸的手机.开发适配小尺寸手机app只需要考虑怎么将控件布局到Activity中,怎样打开一个新的Activity等就可以了.然而Android3.0开始支持平板,屏幕尺寸增大到10寸.这在很大程度

Android中Fragment的简单介绍

Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块. 其中有个经典图,大家就字面上理解下就行: 可以把Fragment设计成可以在多个Activity中复用的模块,为了在Android上创建动态的.多窗口的用户交互体验,你需要将UI组件和