Tabhost与Fragment的结合使用
来源:互联网 发布:mac打不开英雄联盟 编辑:程序博客网 时间:2024/05/12 13:14
主界面布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_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/border" > <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布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:padding="3dp" android:src="@drawable/tab_home_pre"> </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页"> </TextView></LinearLayout>主Activity中的部分代码:
package com.activity;import android.annotation.SuppressLint;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.support.v4.app.FragmentTransaction;import android.util.Log;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TabHost;import android.widget.TextView;import android.widget.Toast;import com.beidougd.bdgc.R;import com.umeng.update.UmengUpdateAgent;public class FragmentMainActivity extends FragmentActivity { private ImageView imageView;private TextView textView;private long touchTime = 0;private static FragmentActivity activity;//定义FragmentTabHost对象private FragmentTabHost mTabHost;//定义一个布局private LayoutInflater layoutInflater;//定义数组来存放Fragment界面private Class fragmentArray[] = {TabHome.class,TabHistory.class,TabFind.class,TabSetting.class};//定义数组来存放按钮图片private int mImageViewArray[] = {R.drawable.tab_home_selector,R.drawable.tab_history_selector,R.drawable.tab_find_selector,R.drawable.tab_setting_selector};//Tab选项卡的文字private String mTextviewArray[] = {"首页", "历史", "发现", "设置"};@SuppressLint("NewApi") @Overrideprotected void onCreate(Bundle savedInstanceState) {requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);activity = this;initViews();UmengUpdateAgent.update(this);//接收通知打开历史界面getIntentandopenHistory();}private void initViews() {MyApplication.getIntance().addActivity(FragmentMainActivity.this);//实例化布局对象layoutInflater = LayoutInflater.from(this);//实例化TabHost对象,得到TabHostmTabHost = (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按钮设置图标、文字和内容TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));//将Tab按钮添加进Tab选项卡中mTabHost.addTab(tabSpec, fragmentArray[i], null);点击下载上述完整代码块。
点击下载完整项目代码。
0 0
- Tabhost与Fragment的结合使用
- android Fragment TabHost 的使用
- Fragment与ActionBar的结合使用
- ViewPager与Fragment结合的简单使用
- [基础学习]ViewPager和Fragment与RadioButton结合做一个类似Tabhost的效果
- TabHost与RadioGroup结合完成的菜单
- TabHost与RadioGroup结合完成的菜单
- TabRadioDemo实现tabhost与RadioBox的结合
- TabHost与RadioGroup结合完成的菜单
- TabHost与RadioGroup结合完成的菜单
- ViewPager与Fragment结合使用
- Fragment与RadioButton结合使用
- Fragment与RadioButton结合使用
- TabHost+Fragment使用
- 使用Fragment替换TabHost
- fragment与viewPager的结合
- Fragment返回栈与Activity任务栈的结合使用
- ViewPager+Fragment的结合使用
- 应用r.js来优化你的前端
- 逗号表达式 result = (a+4,b+3,a+b) ...result = a+b;
- 关于block的一些知识
- 2016继续
- [课上练习]对CreateFile函数的跟踪
- Tabhost与Fragment的结合使用
- 读《android开发艺术探索》笔记一:View的事件分发机制
- 技术团队底蕴判定
- 鸟哥私房菜学习笔记-1
- mariadb 10.1.10安装
- Linux大数据开发基础:第五节:vi、vim编辑器(一)
- 天声人語 20160104 余得なしのカレンダー
- 文章标题
- 嵌入式产品使用核心模块相对单板电脑的好处