标签:
第一步:写main xml文件
<android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="bottom"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> </android.support.v4.app.FragmentTabHost>
<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"> <TextView android:id="@+id/tab_tv_text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="40sp" tools:text="Test"/> </LinearLayout>
<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"> <ImageView android:id="@+id/tab_iv_image" android:padding="12dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@null" tools:src="@drawable/tab_assistant"/> </LinearLayout>
第二步:在mainactivity中设置图片和文字资源
// 图片 @DrawableRes private int mImages[] = { R.drawable.tab_counter, R.drawable.tab_assistant, R.drawable.tab_contest, R.drawable.tab_center }; // 标题 private String mFragmentTags[] = { "counter", "assistant", "contest", "center" };
private View getImageView(int index) { @SuppressLint("InflateParams") View view = getLayoutInflater().inflate(R.layout.view_tab_indicator, null); ImageView imageView = (ImageView) view.findViewById(R.id.tab_iv_image); imageView.setImageResource(mImages[index]); return view; }
tab选中效果
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tab_assistant_light" android:state_selected="true"/> <item android:drawable="@drawable/tab_assistant_gray" android:state_selected="false"/> </selector>
第三步:MainActivity中实例化控件,这里使用了一个插件,叫做butterknife。极大省略了findviewbyid 这样的重复性代码。这样就可以使用mtabhost了
@Bind(android.R.id.tabhost) FragmentTabHost mTabHost;
ButterKnife.bind(this)
第四步:写 oncreate方法
先设置fragment manager
然后给tab设置图片和文字,添加fragment
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);//fragment管理 mTabHost.getTabWidget().setDividerDrawable(null); // 去掉分割线 for (int i = 0; i < mImages.length; i++) { // Tab按钮添加文字和图片 TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mFragmentTags[i]).setIndicator(getImageView(i)); // 添加Fragment mTabHost.addTab(tabSpec, FragmentTab.class, null); // 设置Tab按钮的背景 mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.pedo_actionbar_bkg); } }
第五步:写fragment
@Bind(R.id.tab_tv_text) TextView mTvText; private View mViewContent; // 缓存视图内容 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (mViewContent == null) { mViewContent = inflater.inflate(R.layout.fragment_tab, container, false); } // 缓存View判断是否含有parent, 如果有需要从parent删除, 否则发生已有parent的错误. ViewGroup parent = (ViewGroup) mViewContent.getParent(); if (parent != null) { parent.removeView(mViewContent); } ButterKnife.bind(this, mViewContent); return mViewContent; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // 显示Fragment的Tag信息 mTvText.setText(String.valueOf("Page: " + getTag())); } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.unbind(this); }
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
标签:
原文地址:http://www.cnblogs.com/forri/p/5424095.html