1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 学习笔记 Tianmao 篇 FragmentTabHost (TabHost升级版)

学习笔记 Tianmao 篇 FragmentTabHost (TabHost升级版)

时间:2023-09-10 04:18:30

相关推荐

学习笔记 Tianmao 篇        FragmentTabHost (TabHost升级版)

1.首先创建一个Activity继承自FragmentActivity和与之匹配的layout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><!--这里是真正的fragment--><FrameLayoutandroid:id="@+id/realtabcontent"android:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1" /><!--这里的id是固定的 详情可以看我前面关于TabHost的文章--><android.support.v4.app.FragmentTabHostandroid:id="@android:id/tabhost"android:layout_height="wrap_content"android:layout_width="match_parent"><!--这里是假的fragment 不使用他--><!--这里的id是固定的 详情可以看我前面关于TabHost的文章--><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="0dp"android:layout_height="0dp"android:layout_weight="0" /></android.support.v4.app.FragmentTabHost></LinearLayout>

FragmentActivity里的操作步骤

1.实例化fragmenttabhost

fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

2.这里是与tab同步切换的实际fragment

fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

3.创建以标题为参数的 tabSpec对象

TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(标题);

4.设置tabSpec的视图

layoutInflater = LayoutInflater.from(this);View view = layoutInflater.inflate(R.layout.tab_indicator, null);textView = (TextView) view.findViewById(R.id.txt_indicator);imageView = (ImageView) view.findViewById(R.id.icon_tab);textView.setText(tabBean.getTitle());imageView.setImageResource(tabBean.getIcon());tabSpec.setIndicator(view);

5.添加实际tabSpec和与之对应的Fragment

fragmentTabHost.addTab(tabSpec, fragment.class, null);

然后我们要自己写与之对应的fragment

这个就不多讲了 后面会贴出代码

代码区

HomeActivity.java pers.xxxxx.tianmao.ui.activity;

public class HomeActivity extends FragmentActivity {private FragmentTabHost fragmentTabHost;private LayoutInflater layoutInflater;private TextView textView;private ImageView imageView;private List<TabBean> list = new ArrayList<>(5);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_loading);initTab();}private void initTab() {//初始化listTabBean tab_home = new TabBean(HomeFragment.class, R.drawable.selector_icon_home, R.string.home);TabBean tab_care = new TabBean(HotFragment.class, R.drawable.selector_icon_care, R.string.care);TabBean tab_tmzb = new TabBean(CategoryFragment.class, R.drawable.selector_icon_tmzb, R.string.tmzb);TabBean tab_cart = new TabBean(CartFragment.class, R.drawable.selector_icon_cart, R.string.cart);TabBean tab_mine = new TabBean(MineFragment.class, R.drawable.selector_icon_mine, R.string.mine);list.add(tab_home);list.add(tab_care);list.add(tab_tmzb);list.add(tab_cart);list.add(tab_mine);//实例化fragmenttabhostfragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);// 这里是与tab同步切换的实际fragmentfragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);for (TabBean tabBean : list) {//标题TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(String.valueOf(tabBean.getTitle()));//消除分割线fragmentTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);layoutInflater = LayoutInflater.from(this);View view = layoutInflater.inflate(R.layout.tab_indicator, null);textView = (TextView) view.findViewById(R.id.txt_indicator);imageView = (ImageView) view.findViewById(R.id.icon_tab);textView.setText(tabBean.getTitle());imageView.setImageResource(tabBean.getIcon());tabSpec.setIndicator(view);fragmentTabHost.addTab(tabSpec, tabBean.getFragment(), null);}}}

TabBean.java pers.lijunxue.tianmao.ui.javabean;

public class TabBean {private int title; //每个图片按钮的一个标志private int icon; //图片private Class fragment; //必须加载的fragment,类似于HomeFragmentpublic TabBean(Class fragment, int icon, int title){ this.fragment = fragment;this.icon = icon;this.title = title;}public int getIcon(){return icon;}public void setIcon(int icon){this.icon = icon;}public int getTitle(){return title;}public void setTitle(int title){this.title = title;}public Class getFragment(){return fragment;}public void setFragment(Class fragment){this.fragment = fragment;}}

selector_icon_care.xml 举一个例子 其他几个都一样的

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android"><!-- Non focused states --><item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_care_n" /><item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_care_press" /><!-- Focused states --><item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_care_press" /><item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_care_press" /><!-- Pressed --><item android:state_selected="true" android:state_pressed="true" android:drawable="@mipmap/icon_care_press" /><item android:state_pressed="true" android:drawable="@mipmap/icon_care_press" /></selector>

大家还记得上面有这样一句代码吧layoutInflater.inflate(R.layout.tab_indicator, null);

tab_indicator.xml

<!--这个布局是fragmentTabHost中一个按钮的布局--><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center"android:paddingTop="3dp"android:paddingBottom="3dp"android:gravity="center"><ImageViewandroid:id="@+id/icon_tab"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/txt_indicator"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:textColor="@color/selector_tab_text"/></LinearLayout>

fragment 空白的类 同学们可以自己添加view

public class HomeFragment extends Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_home, container, false);return view;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。