FragmentTabHost
fragmentTabHost如果在AndroidStudio里面使用的话不需要导入任何包 如果在eclipse里面使用要导入一个包support-v4-19.0.0.jar 路径就是在
E:\androidSDK\android-sdk_r24.4.1-windows\android-sdkwindows\extras\android\m2repository\com\android\support\support-v4\19.0.0
使用:
Fragment分为上下按钮分布
上面的格式 上下格式全部是固定的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/fragmentContentId"> </FrameLayout> <android.support.v4.app.FragmentTabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fragmentTabHostId"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs">
<!--这里的Id必须一样的是android内部的Id才可以-->
</TabWidget> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabcontent"> </FrameLayout> </LinearLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
下布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/fragmentContentId"> </FrameLayout> <android.support.v4.app.FragmentTabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fragmentTabHostId"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="0dp" android:layout_height="0dp" android:id="@android:id/tabcontent"> </FrameLayout> </LinearLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
在代码中初始化
package com.liwangjiang.fragmentTabHostName; import com.liwangjiang.fragmentClass.OneFragment; import com.liwangjiang.fragmentClass.ThereFragment; import com.liwangjiang.fragmentClass.TwoFragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.util.Log; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class MainActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.找到Fragment对象 FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); //2.对这个FragmentTabHost进行初始化 //containertId就是我梦将要显示Fragment的容器 tabHost.setup(this,getSupportFragmentManager() , android.R.id.tabcontent);//初始化FragmentTabHost //3.每个FragmentTagHost添加选项卡 TabHost.TabSpec one=tabHost.newTabSpec("1");//这个字符是代表这个选项卡的标识符 one.setIndicator("One");//代表显示的内容 TabHost.TabSpec two=tabHost.newTabSpec("2");//这个字符是代表这个选项卡的标识符 one.setIndicator("two");//代表显示的内容 TabHost.TabSpec there=tabHost.newTabSpec("3");//这个字符是代表这个选项卡的标识符 one.setIndicator("there");//代表显示的内容 //设置接听器 tabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { Log.v("liwangjiang", ""+tabId); } }); //设置当前页面的方法 //根据标签的角标来设置 tabHost.setCurrentTab(1);//表示设置当前显示第0-1-2 第1页 //根据TabSpecTag来设置 tabHost.setCurrentTabByTag("2"); tabHost.addTab(one,OneFragment.class,null); tabHost.addTab(two,TwoFragment.class,null); tabHost.addTab(there,ThereFragment.class,null); } }
private void initFragmentTabHost(){ //找到FragmentTabHost FragmentTabHost tabHost=findViewById(R.id.fragmentTabHostId); //对FragmentTabHost进行初始化 tabHost.setup(this,getSupportFragmentManager(),R.id.fragmentContentId); String[] titles=getResources().getStringArray(R.array.tab_title); int[] imageViewResource = new int[]{R.drawable.news_selector,R.drawable.reading_selector,R.drawable.video_selector,R.drawable.topic_selector,R.drawable.mine_selector}; Class[] clz = new Class[]{NewFragment.class, EmptyFragment.class,NewFragment.class ,EmptyFragment.class, EmptyFragment.class}; for (int i=0;i<titles.length;i++){ TabHost.TabSpec tmp=tabHost.newTabSpec(""+i); tmp.setIndicator(getEmptyView(this,titles,imageViewResource,i)); tabHost.addTab(tmp, clz[i],null); } //为每个Fragment添加选项 // TabHost.TabSpec one=tabHost.newTabSpec("0"); //one.setIndicator("one");//显示的内容 //one.setIndicator(getEmptyView(this)); } private View getEmptyView(Context context,String[] title,int[] imageViews,int index){ LayoutInflater inflater = LayoutInflater.from(context); View view=inflater.inflate(R.layout.item_title,null); //设置文字 TextView textView=view.findViewById(R.id.tv_textViewId); textView.setText(title[index]); //设置图片 ImageView imageView=view.findViewById(R.id.iv_ImageView); imageView.setImageResource(imageViews[index]); return view; }