本文主要是介绍FragmentTabHost实现app底部tab功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Demo下载连接: 点击打开链接
FragmentTabHost是继承之TabHost的 但是TabHost已经过时 FragmentTabHost是结合fragment一起使用的 在使用的过程中需要注意 Activity必须是继承之FragmentActivity的 否则会报错
使用FragmentTabHost主要流程步骤:
每一个tab通过enum进行定义 代码如下:
public enum MainTab {NEWS(0, R.string.main_tab_name_news, R.drawable.tab_icon_new,NewsViewPagerFragment.class),TWEET(1, R.string.main_tab_name_tweet, R.drawable.tab_icon_tweet,TweetsViewPagerFragment.class),ME(2, R.string.main_tab_name_my, R.drawable.tab_icon_me,MyInformationFragment.class);private int idx;private int resName;private int resIcon;private Class<?> clz;private MainTab(int idx, int resName, int resIcon, Class<?> clz) {this.idx = idx;this.resName = resName;this.resIcon = resIcon;this.clz = clz;}public int getIdx() {return idx;}public void setIdx(int idx) {this.idx = idx;}public int getResName() {return resName;}public void setResName(int resName) {this.resName = resName;}public int getResIcon() {return resIcon;}public void setResIcon(int resIcon) {this.resIcon = resIcon;}public Class<?> getClz() {return clz;}public void setClz(Class<?> clz) {this.clz = clz;}
}
MainActivity的代码:
public class MainActivity extends AppCompatActivity {private FragmentTabHost mTabHost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.inject(this);initView();}private void initView() {mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);if (android.os.Build.VERSION.SDK_INT > 10) { //去除tab之间的竖线mTabHost.getTabWidget().setShowDividers(0);}initTab();}private void initTab() {MainTab[] tabs = MainTab.values();for (int i = 0; i < tabs.length; i++) {MainTab mainTab = tabs[i];TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));View indicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);TextView tabTitle = (TextView) indicator.findViewById(R.id.tab_title);tabTitle.setText(getString(mainTab.getResName()));tabTitle.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(mainTab.getResIcon()), null, null);tab.setIndicator(indicator);tab.setContent(new TabHost.TabContentFactory() {@Overridepublic View createTabContent(String tag) {return new View(MainActivity.this);}});mTabHost.addTab(tab, mainTab.getClz(), null);}}
}
布局文件 activtiy_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:id="@+id/realtabcontent"android:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="4dip"><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:background="#c8c8c8"/><android.support.v4.app.FragmentTabHostandroid:id="@+id/tabhost"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="4dip" /></RelativeLayout></LinearLayout>
这篇关于FragmentTabHost实现app底部tab功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!