fragment实现android tab
来源:互联网 发布:小天才手表网络不可用 编辑:程序博客网 时间:2024/05/13 23:56
参考:
http://www.blogjava.net/amplifier/archive/2012/12/27/393409.html
http://developer.android.com/reference/android/app/TabActivity.html
http://developer.android.com/training/basics/fragments/index.html
步骤:
1:activity修改为extends FragmentActivity
2:在activity类中定义private FragmentTabHost mTabHost;
3:定义tab的layout文件:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> </LinearLayout></android.support.v4.app.FragmentTabHost>
4:修改activity的oncreate函数:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_tab);mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("Simple").setIndicator("sss"), Tab1Fragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), Tab2Fragment.class, null);/* mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"), LoaderCustomSupport.AppListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);*/}
其中的Tab1Fragment和Tab2Fragment在后面定义
5:定义Tab1Fragment和Tab2Fragment
不像activity可以用系统提供的向导定义,Fragment需要自己手动定义。
首先定义layout文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text="@string/stringauto" > <requestFocus /> </AutoCompleteTextView></LinearLayout>
然后生成一个新类,这个类派生Fragment,并且使用上面定义的layout:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Tab1Fragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub//return super.onCreateView(inflater, container, savedInstanceState);return inflater.inflate(R.layout.tab1, container, false);}}
同样的方式定义Tab2Fragment
6:运行,查看效果
- fragment实现android tab
- Android UI之Tab(ActionBar+Fragment实现)
- android:使用fragment实现tab切换
- android fragment 实现tab效果 注意事项
- Android Tab实现之FragmentManager+Fragment
- Android利用Fragment实现Tab布局
- Fragment实现tab功能
- 使用fragment实现tab
- Fragment实现Tab
- Fragment实现Tab
- fragment实现Tab效果
- tab+fragment实现主页
- 使用Fragment实现Tab
- Android ViewPager Fragment tab
- Android Fragment制作Tab
- Android tab页选项卡效果Fragment实现
- Android UI 详解之ActionBar+ViewPager+Fragment 实现滑动Tab
- android tab实现方式2 of 4,fragment
- QMakeBuilder - Jenkins plugin for Qt
- 宝石碰碰:HTML5开发Android本地化App游戏案例
- cocos2d-x 避免手打输入项目cpp文件到android.mk里面编译
- [四天学会ajax]学习ajax教程第一天
- D27874916863
- fragment实现android tab
- 基于json的AJAX高级开发技术
- 关于C++中的意外异常和未捕获异常
- 数组、指针作为函数参数
- MySQL my.cnf参数配置优化详解
- 2005. Lovely Number
- C语言迷宫求解(完结版)
- jgGrid获得的id值是主键的id而不是jqGrid的行号值
- Objective-C编程:在类的外部声明全局变量