Android开发Android知识Android技术知识

TabHost+Fragment的应用

2018-01-09  本文已影响0人  一s独秀

很久没有应用了,东西都忘记了,总结出来,曾经的东西在捡起来一次,加强下记忆

上代码:

 <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroudColor">
        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@color/wirteColor"/>
        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/wirteColor">
                <LinearLayout
                    android:id="@+id/line_one"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/line_two"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/line_three"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>
        </FrameLayout>
</TabHost>
TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
//添加“主页”Tab到TabHost控件中
 mTabHost.addTab(mTabHost.newTabSpec("home")//Tag
                .setIndicator("HOME")//设置Tab标签和图标
                .setContent(R.id.line_one));        //设置Tab内容
//添加“消息”Tab到TabHost控件中
 mTabHost.addTab(mTabHost.newTabSpec("news")
                .setIndicator("NEWS")
                .setContent(R.id.line_two));
//添加“个人”Tab到TabHost控件中
mTabHost.addTab(mTabHost.newTabSpec("mine")
                .setIndicator("MINE")
                .setContent(R.id.line_three));
//设置当前默认显示第一个Tab
mTabHost.setCurrentTab(0);
 //TabHost改变监听
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        Log.d("===>>>onTabChanged",tabId.toString());
    }
});
//home
HomeFragment nomeFragment= new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_one, homeFragment).commit();
//news
NewsFragment newsFragment = new NewsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_two, newsFragment ).commit();
//mine
MineFragment mineFragment= new MineFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_three, mineFragment).commit();

<!--        === layout中 ===          -->
//只在布局中添加下面就行了,再去drawable创建选中时的字体颜色--home_item_selected
<TextView
        android:textSize="@dimen/sp18"
        android:textColor="@drawable/home_item_selected"
        android:text="@string/string_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<!--        === drawable中 ===          -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/appthemeColor" />
    <item android:color="@color/textColor" />
</selector>

好了,就这么简单的完了,当然了,TabHost+Fragment的写法有好几种,我这里只用了最简单的这一种,当然,现在由于项目的需求,有些也用不上此方法,因为这个加载数据是一次性全部执行,三个Fragment的生命周期一起执行的

上一篇 下一篇

猜你喜欢

热点阅读