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:运行,查看效果

原创粉丝点击