FragmentTabHost
来源:互联网 发布:金润软件成都 编辑:程序博客网 时间:2024/04/30 08:24
首先布局
第一个FrameLayout 是用来显示内容的桢布局
下面的FragementTabHost是显示标签,标签在上面或者下面取决于布局
<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="com.example.acer_123.fragmenttabhost.MainActivity"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> <android.support.v4.app.FragmentTabHost android:background="@android:color/darker_gray" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v4.app.FragmentTabHost></LinearLayout>在java代码MainActivity中
//初始化FragmentTabHost FragmentTabHost tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost); //关联tabHost和FragmeLayout //getSupportFragmentManager()用于兼容,需要让MainActivity继承v4包里面的FragmentActivity,如果不要兼容,可以换成getFragmentManager() tabhost.setup(MainActivity.this,getSupportFragmentManager(),android.R.id.tabcontent); for (int i = 0; i < texts.length; i++) { //创建Tab TabHost.TabSpec tabSpec = tabhost.newTabSpec("选项"+(i+1)); //标签视图,可以自由定制 TextView textView = new TextView(this); textView.setGravity(Gravity.CENTER); textView.setText(texts[i]); //在TextView上面设置图片 textView.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.ic_launcher),null,null); tabSpec.setIndicator(textView); //把bundle信息传递到fragment去 Bundle bundle = new Bundle(); bundle.putString("data",texts[i]); //添加Tab tabhost.addTab(tabSpec,BaseFragment.class,bundle);
BaseFragment 继承Fragment
public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //fragment接收通过bundle传递来的信息 Bundle bundle = getArguments(); if (bundle!=null){ data = bundle.getString("data");
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (textView==null){ textView = new TextView(getActivity()); textView.setText(data); //延迟2秒执行代码,用来检验fragment的缓存 new Handler().postDelayed(new Runnable() { @Override public void run() { textView.setText("哈哈"); } },2000); }else { //实现fragment缓存的代码 ViewParent parent = textView.getParent(); if (parent !=null && parent instanceof ViewGroup){ ViewGroup group = (ViewGroup) parent; group.removeView(textView); } } return textView;效果图
0 0
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabhost
- FragmentTabHost
- 【FragmentTabHost】FragmentTabHost的使用
- FragmentTabHost简介
- FragmentTabHost用法
- Android FragmentTabHost
- android fragmenttabhost
- FragmentTabHost 使用;
- mysql 随机RAND和GROUP BY优化
- VIM的高级用法
- 关于ActivityManagerService的学习启动模式 7.23
- Radar Installation
- CentOs nginx 安装 及 负载均衡配置
- FragmentTabHost
- 从web专业角度谈谈如何对screen设备宽度断点
- js注意事项03
- 解决 source insight 代码关联问题
- 第三天 原串翻转
- 某公司泄露版IDA pro6.8去除局域网检测
- Stage4--Python面向对象
- HDU_多重背包系列
- POJ 2774 Long Long Message (后缀数组求最长公共子串)