2013.08.15——— android Fragment的简单实用
参考:http://www.eoeandroid.com/thread-205166-1-1.html

<TabHost    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="@+id/realtabcontent"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"/>                  <TabWidget            android:id="@android:id/tabs"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:tabStripEnabled="false"            android:background="@color/main_bg_color"            android:layout_weight="0"/>                <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0"/>    </LinearLayout></TabHost>



id为realtabcontent的就是用来当fragment容器的,这个例子用了TabWidget,我一般都是用RadioButton来替代这个

package com.mostmood.activity;import com.mostmood.R;import com.mostmood.fragment.Home_Fragment;import com.mostmood.fragment.Me_Fragment;import com.mostmood.fragment.Message_Fragment;import com.mostmood.fragment.Wall_Fragment;import com.mostmood.util.DummyTabContent;import android.app.Dialog;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.WindowManager;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TextView;import android.support.v4.app.FragmentActivity;/** * @Description: 主Activity,底部TabHost选项卡 * * @Author Hades * * @date 2012-10-6 * * @version V1.0   */public class MainActivity extends FragmentActivity {TabHost tabHost;TabWidget tabWidget; LinearLayout bottom_layout;int CURRENT_TAB = 0;//设置常量Home_Fragment homeFragment;Wall_Fragment wallFragment;Message_Fragment messageFragment;Me_Fragment meFragment;android.support.v4.app.FragmentTransaction ft;RelativeLayout tabIndicator1,tabIndicator2,tabIndicator3,tabIndicator4,tabIndicator5;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findTabView();        tabHost.setup();                /** 监听*/        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener(){@Overridepublic void onTabChanged(String tabId) {/**碎片管理*/android.support.v4.app.FragmentManager fm =  getSupportFragmentManager();homeFragment = (Home_Fragment) fm.findFragmentByTag("home");wallFragment = (Wall_Fragment) fm.findFragmentByTag("wall");messageFragment = (Message_Fragment) fm.findFragmentByTag("message");meFragment = (Me_Fragment) fm.findFragmentByTag("me");ft = fm.beginTransaction();/** 如果存在Detaches掉 */if(homeFragment!=null)ft.detach(homeFragment);/** 如果存在Detaches掉 */if(wallFragment!=null)ft.detach(wallFragment);/** 如果存在Detaches掉 */if(messageFragment!=null)ft.detach(messageFragment);/** 如果存在Detaches掉 */if(meFragment!=null)ft.detach(meFragment);/** 如果当前选项卡是home */if(tabId.equalsIgnoreCase("home")){isTabHome();CURRENT_TAB = 1;/** 如果当前选项卡是wall */}else if(tabId.equalsIgnoreCase("wall")){isTabWall();CURRENT_TAB = 2;/** 如果当前选项卡是message */}else if(tabId.equalsIgnoreCase("message")){isTabMessage();CURRENT_TAB = 3;/** 如果当前选项卡是me */}else if(tabId.equalsIgnoreCase("me")){isTabMe();CURRENT_TAB = 4;}else{switch (CURRENT_TAB) {case 1:isTabHome();break;case 2:isTabWall();break;case 3:isTabMessage();break;case 4:isTabMe();break;default:isTabHome();break;}}ft.commit();}                };        //设置初始选项卡          tabHost.setCurrentTab(0);        tabHost.setOnTabChangedListener(tabChangeListener);        initTab();         /**  设置初始化界面  */        tabHost.setCurrentTab(0);    }        //判断当前    public void isTabHome(){        if(homeFragment==null){ft.add(R.id.realtabcontent,new Home_Fragment(), "home");}else{ft.attach(homeFragment);}    }        public void isTabWall(){        if(wallFragment==null){ft.add(R.id.realtabcontent,new Wall_Fragment(), "wall");}else{ft.attach(wallFragment);}    }        public void isTabMessage(){        if(messageFragment==null){ft.add(R.id.realtabcontent,new Message_Fragment(), "message");}else{ft.attach(messageFragment);}    }        public void isTabMe(){        if(meFragment==null){ft.add(R.id.realtabcontent,new Me_Fragment(), "me");}else{ft.attach(meFragment);}    }    /**     * 找到Tabhost布局     */    public void findTabView(){         tabHost = (TabHost) findViewById(android.R.id.tabhost);         tabWidget = (TabWidget) findViewById(android.R.id.tabs);         LinearLayout layout = (LinearLayout)tabHost.getChildAt(0);         TabWidget tw = (TabWidget)layout.getChildAt(1);                  tabIndicator1 = (RelativeLayout) LayoutInflater.from(this)         .inflate(R.layout.tab_indicator, tw, false);         TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);         ImageView ivTab1 = (ImageView)tabIndicator1.getChildAt(0);         ivTab1.setBackgroundResource(R.drawable.selector_mood_home);         tvTab1.setText(R.string.buttom_home);                  tabIndicator2 = (RelativeLayout) LayoutInflater.from(this)         .inflate(R.layout.tab_indicator, tw, false);         TextView tvTab2 = (TextView)tabIndicator2.getChildAt(1);         ImageView ivTab2 = (ImageView)tabIndicator2.getChildAt(0);         ivTab2.setBackgroundResource(R.drawable.selector_mood_wall);         tvTab2.setText(R.string.buttom_wall);                  tabIndicator3 = (RelativeLayout) LayoutInflater.from(this)         .inflate(R.layout.tab_indicator_camera, tw, false);         TextView tvTab3 = (TextView)tabIndicator3.getChildAt(1);         ImageView ivTab3 = (ImageView)tabIndicator3.getChildAt(0);         ivTab3.setBackgroundResource(R.drawable.selector_mood_photograph);         tvTab3.setText(R.string.buttom_camera);                   tabIndicator4 = (RelativeLayout) LayoutInflater.from(this)         .inflate(R.layout.tab_indicator, tw, false);         TextView tvTab4 = (TextView)tabIndicator4.getChildAt(1);         ImageView ivTab4 = (ImageView)tabIndicator4.getChildAt(0);         ivTab4.setBackgroundResource(R.drawable.selector_mood_message);         tvTab4.setText(R.string.buttom_message);                  tabIndicator5 = (RelativeLayout) LayoutInflater.from(this)         .inflate(R.layout.tab_indicator, tw, false);         TextView tvTab5 = (TextView)tabIndicator5.getChildAt(1);         ImageView ivTab5 = (ImageView)tabIndicator5.getChildAt(0);         ivTab5.setBackgroundResource(R.drawable.selector_mood_my_wall);         tvTab5.setText(R.string.buttom_me);    }        /**      * 初始化选项卡     *      * */    public void initTab(){            TabHost.TabSpec tSpecHome = tabHost.newTabSpec("home");        tSpecHome.setIndicator(tabIndicator1);                tSpecHome.setContent(new DummyTabContent(getBaseContext()));        tabHost.addTab(tSpecHome);                TabHost.TabSpec tSpecWall = tabHost.newTabSpec("wall");        tSpecWall.setIndicator(tabIndicator2);                tSpecWall.setContent(new DummyTabContent(getBaseContext()));        tabHost.addTab(tSpecWall);                TabHost.TabSpec tSpecCamera = tabHost.newTabSpec("camera");        tSpecCamera.setIndicator(tabIndicator3);                tSpecCamera.setContent(new DummyTabContent(getBaseContext()));        tabHost.addTab(tSpecCamera);                //拍照按钮监听事件,弹出dialog        tabIndicator3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Dialog choose = new Dialog(MainActivity.this,R.style.draw_dialog);    choose.setContentView(R.layout.camera_dialog);    // 设置背景模糊参数WindowManager.LayoutParams winlp = choose.getWindow().getAttributes();winlp.alpha = 0.9f; // 0.0-1.0choose.getWindow().setAttributes(winlp);choose.show();// 显示弹出框}});                TabHost.TabSpec tSpecMessage = tabHost.newTabSpec("message");        tSpecMessage.setIndicator(tabIndicator4);              tSpecMessage.setContent(new DummyTabContent(getBaseContext()));        tabHost.addTab(tSpecMessage);                TabHost.TabSpec tSpecMe = tabHost.newTabSpec("me");        tSpecMe.setIndicator(tabIndicator5);                tSpecMe.setContent(new DummyTabContent(getBaseContext()));        tabHost.addTab(tSpecMe);            }    }


点击选项卡,然后就new 一个fragment,并且添加进去刚才那个id为realtabcontent的framelayout

看一下fragment怎么写的
package com.mostmood.fragment;import com.mostmood.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Home_Fragment 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 stubreturn inflater.inflate(R.layout.moodhome, container, false);}}




更多相关文章

  1. 常见的C++中const常量用法分析讲解
  2. 区分C++常量表达式、const、constexpr(附代码)
  3. C++学习基础知识--this指针、静态成员、常量成员函数
  4. 指针常量与常量指针举例说明
  5. c语言常量定义规则
  6. c语言中合法的字符常量如何定义?
  7. c语言常量的正确表示方法有哪些
  8. c语言字符常量的合法表示形式是什么
  9. c++数组初始化的种类有哪些

随机推荐

  1. TextView常见功能应用
  2. Android(安卓)上Camera分析
  3. android实现自动对焦拍照
  4. Android(安卓)面试必备 - JVM 及 类加载
  5. Android深入浅出系列课程---Lesson2 AAF1
  6. React-Native测试报告
  7. Android(安卓)X86
  8. android:gravity与android:layout_gravit
  9. android UI 配置参数
  10. 初学Android学到的关于LinearLayout的知