现在的位置: 首页 > 综合 > 正文

TabHost基本实现(二)

2018年06月05日 ⁄ 综合 ⁄ 共 7442字 ⁄ 字号 评论关闭

TabHost基本实现(二)

一、回顾

上一节实现了TabHost基本功能,但是由于是android提供的标签,是的对于每个tab样式修改,不灵活,本章,将简单说明本人实现方式,TabHost+fragment。此处我实现的fragment方式,是使用的静态Fragment.以及,自己定义tab布局,代替使用android提供的TabWidget。
废话不多说,直接主题。

二、具体代码

1、准备工作
       创建四个Fragment文件,RecommendateFragment.java、ShareFragment.java、                             ActivityFragment.java、MeFragment.java
由于代码简单只附上部分代码
public class RecommendateFragment extends Fragment {
	
	  
	 @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	 
	 @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		 
		  View view=inflater.inflate(R.layout.recommendate_fragment, container,false);
		return view;
	}
	 
	 @Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
	}

2、创建布局文件main_tabhost.xml

 <TabHost
    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"
        android:background="@color/app_background">

       <!--tabcontent:显示tab内容部分  -->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0.0dip"
                android:layout_weight="1.0" >

                <fragment
                    android:id="@+id/share_fragment"
                    android:name="com.qudong.myproject.ShareFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
               
                <fragment
                    android:id="@+id/activity_fragment"
                    android:name="com.qudong.myproject.ActivityFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                <fragment
                    android:id="@+id/me_fragment"
                    android:name="com.qudong.myproject.MeFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

                <fragment
                    android:id="@+id/recommendation_fragment"
                    android:name="com.qudong.myproject.RecommendateFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </FrameLayout>
            
         
       <!-- 选项卡组件不用,隐藏 -->
		  <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"/>
		  
		  <!--tab按钮  -->
		<!--自定义各个选项卡  -->
	  <FrameLayout
          android:id="@+id/layout_bottom_camera"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content">  
		  
	    <FrameLayout
                    android:id="@+id/layout_bottom"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom" >
		  
          <LinearLayout
               android:id="@+id/classifies_layout"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_gravity="bottom"
               android:gravity="center"
               android:orientation="horizontal" >
				<!--tab1  -->
                <LinearLayout
                    android:id="@+id/recommendation_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_weight="0.2"
                    android:orientation="vertical">

                    <ImageButton
                         android:id="@+id/recommendation_img"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_gravity="center_horizontal"
                         android:background="@drawable/home" />

                     <TextView
                                android:id="@+id/recommendation_txt"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="2dip"
                                android:gravity="center"
                                android:layout_gravity="center_horizontal"
                                android:text="@string/used_phones"
                                android:textColor="@color/sports_sk_selected"
                                android:textSize="12sp" />
                </LinearLayout>
                        <!--tab2  -->
              <LinearLayout
                    android:id="@+id/share_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_weight="0.2"
                    android:orientation="vertical">

                    <ImageButton
                       android:id="@+id/share_img"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center_horizontal"
                       android:background="@drawable/home" />

                    <TextView
                       android:id="@+id/share_txt"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_marginTop="2dip"
                       android:gravity="center"
                       android:layout_gravity="center_horizontal"
                       android:text="@string/used_phones"
                       android:textColor="@color/sports_sk_selected"
                       android:textSize="12sp" />
                </LinearLayout>
              
                   <!--tab3 只是用来站位-->
                   
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_weight="0.2"
                    android:visibility="visible"
                    android:orientation="vertical">

                    <ImageButton
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center_horizontal"
                       android:background="@drawable/home"/>
                     <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dip"
                        android:gravity="center"
                        android:text="@string/used_phones"
                        android:layout_gravity="center_horizontal"
                        android:textColor="@color/sports_sk_selected"
                        android:textSize="12sp" />
                </LinearLayout>
                   
                  <!--tab4 -->
                <LinearLayout
                    android:id="@+id/activity_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_weight="0.2"
                    android:orientation="vertical">

                    <ImageButton
                        android:id="@+id/activity_img"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:background="@drawable/home" />

                    <TextView
                        android:id="@+id/activity_txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dip"
                        android:gravity="center"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/used_phones"
                        android:textColor="@color/sports_sk_selected"
                        android:textSize="12sp" />
                </LinearLayout>
                    
				<!-- tab5 -->
			    <LinearLayout
                    android:id="@+id/me_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_weight="0.2"
                    android:orientation="vertical">

                    <ImageButton
                       android:id="@+id/me_img"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center_horizontal"
                       android:background="@drawable/home" />

                    <TextView
                       android:id="@+id/me_txt"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_marginTop="2dip"
                       android:gravity="center"
                       android:layout_gravity="center_horizontal"
                       android:text="@string/used_phones"
                       android:textColor="@color/sports_sk_selected"
                       android:textSize="12sp" />
                </LinearLayout>
             </LinearLayout>
           </FrameLayout>
       </LinearLayout>
   </TabHost>  

3、MainTabHostActivity.java文件

由于代码简单只附上部分代码
初始化选项卡内容部分
/**
	 * 初始化Tabhost内容部分,初始化Fragment
	 */
	private void initFragments() {
		tabhost = (TabHost) findViewById(android.R.id.tabhost);
		tabhost.setup();
		/**
		 *  在tabHost中创建一个Tab标签:
		 *  
		 * myTabhost
                .addTab(myTabhost.newTabSpec("TT")// 制造一个新的标签TT
                        .setIndicator("KK",
                                getResources().getDrawable(R.drawable.ajjc))
                        // 设置一下显示的标题为KK,设置一下标签图标为ajjc
                        .setContent(R.id.widget_layout_red));//设置一下该标签页的布局内容为R.id.widget_layout_red
        
		 */
		tabhost.addTab(tabhost.newTabSpec("recommendation").setIndicator("recommendation")
				.setContent(R.id.recommendation_fragment));
		tabhost.addTab(tabhost.newTabSpec("share").setIndicator("share")
				.setContent(R.id.share_fragment));
		tabhost.addTab(tabhost.newTabSpec("activity").setIndicator("activity")
				.setContent(R.id.activity_fragment));
		tabhost.addTab(tabhost.newTabSpec("me").setIndicator("me")
				.setContent(R.id.me_fragment));
	}

单击每个tab按钮,要切换每个tab内容只需,在tab单击事件下添加

case R.id.recommendation_layout:
		case R.id.recommendation_img:
		case R.id.recommendation_txt:
			
			//设置当前选项卡
			<span style="color:#ff6600;">tabhost.setCurrentTabByTag("recommendation")</span>


三、运行结果




这样一个简单可灵活改边Tab样式的,TabHost完成,接下来,我们要使用使用了TabManager动态的管理fragment,
及 mTabHost.setOnTabChangedListener(this),监听tab选项卡的变化,及系统的选项卡TabWidget作为选项卡 

抱歉!评论已关闭.