FragmentTabHost的简单应用
来源:互联网 发布:校园网络规划设计 编辑:程序博客网 时间:2024/06/06 02:23
FragmentTabHost继承于TabHost组件,把TabHost和 Fragment有效的结合在了一起
参考:http://blog.csdn.net/yangyu20121224/article/details/9016223
package com.example.lenovo.myapplication;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 yangyu *功能描述:自定义TabHost */public class MainActivity extends FragmentActivity{ //定义FragmentTabHost对象 private FragmentTabHost mTabHost; //定义一个布局 private LayoutInflater layoutInflater; //定义数组来存放Fragment界面 private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class}; //定义数组来存放按钮图片 private int mImageViewArray[] = {R.drawable.tabhome,R.drawable.message}; //Tab选项卡的文字 private String mTextviewArray[] = {"首页", "消息"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_tab_layout); 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.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; }}
main_tab_layout
<?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" /> /<span style="color:#FF0000;"><strong>/如果把这个内容放置在最下面,Tab就在上面了</strong></span> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>
tab_item_view
<?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/tabhome"> </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:textSize="10sp" android:textColor="#ffffff"> </TextView></LinearLayout>fragment_2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitCenter" android:src="@drawable/hydrangeas" > </ImageView> </LinearLayout>
fragment_2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitCenter" android:src="@drawable/desert" > </ImageView> </LinearLayout>
0 0
- FragmentTabHost的简单应用
- FragmentTabHost的应用
- FragmentTabHost的应用
- FragmentTabHost的简单使用
- FragmentTabHost的简单使用
- FragmentTabHost的简单使用
- FragmentTabHost的简单用法
- android FragmentTabHost 的简单使用
- android FragmentTabHost的简单使用
- android FragmentTabHost 的简单使用
- Android-FragmentTabHost的简单使用
- FragmentTabHost简单的主流引用
- FragmentTabHost实现底部切换简单应用
- 【FragmentTabHost】FragmentTabHost的使用
- android开发之-FragmentTabHost的应用
- FragmentTabHost简单保存状态的方法
- FragmentTabHost简单保存状态的方法
- 一种简单的底部导航fragmentTabhost
- Android按两次返回键退出程序
- 生存能力
- WPF中MainWindow与Page通信
- 九度OJ 1043:Day of Week(星期几) (日期计算)
- HDU 5505 GT and numbers(求一个数乘以它的因子得到另一个数的最小步数)
- FragmentTabHost的简单应用
- eclipse -- 工作中经常用到的快捷键(不断完善)
- 境外自然人不得以实际出资为由主张转让其在国内公司中享有的股权
- Android开发规范
- 操你妈的 太麻烦了
- linux-ht开启
- Java基础--面向对象
- Swap Nodes in Pairs
- POJ 2386 Lake Counting (DFS or BFS)