<Android 基础(三十八)> FragmentTabHost
来源:互联网 发布:全平台主播数据榜单 编辑:程序博客网 时间:2024/05/16 04:54
1. 简介
FragmentTabHost是一种特殊的TabHost,允许我们在Tab内容区域内使用Fragment。当我们得到FragmentTabHost对象后,我们必须通过调用setup(Context, FragmentManager, int)方法来完成对tabhost的初始化。
FragmentTabHost是Android4.0版本的控件, 之前被广泛的使用,但是我尽然没有使用过,因为我开始学习Android的时候版本已经来到了5.0版本,使用的比较多的是TabLayout+ViewPager这种方式。但是作为基础知识,FragmentTabHost也是需要学习一下的。
2. 类结构
FragmentTabHost继承TabHost。先看一下public方法
3. 基本使用
3.1 布局文件
activity_main.xml
注意:
android:id=”@android:id/tabhost”
android:id=”@android:id/tabcontent”
android:id=”@android:id/tabs”
<?xml version="1.0" encoding="utf-8"?><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"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="bottom"/> </LinearLayout></android.support.v4.app.FragmentTabHost>
fragment_content.xml
<?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"> <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>
indicator.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/iv_indicator" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5"/> <TextView android:id="@+id/tv_indicator" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2.5" android:gravity="center" android:textSize="5pt" /></LinearLayout>
3.1 代码
MainActivity.java
public class MainActivity extends AppCompatActivity { // 图片 @DrawableRes private int mImages[] = { R.drawable.tab_daily, R.drawable.tab_sort, R.drawable.tab_bonus, R.drawable.tab_about }; // 标题 private String mFragmentTags[] = { "每日干货", "分类阅读", "福利社区", "关于作者" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentTabHost fragmentTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); fragmentTabHost.setup(this,getSupportFragmentManager(), android.R.id.tabcontent); for (int i = 0; i < mImages.length; i++) { TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(mFragmentTags[i]).setIndicator(getImageView(i)); fragmentTabHost.addTab(tabSpec, YFragment.class, null); fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.colorWhite); } fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String s) { Toast.makeText(MainActivity.this, "OnTabChanged To : " + s, Toast.LENGTH_SHORT).show(); } }); } // 获得图片资源 private View getImageView(int index) { @SuppressLint("InflateParams") View view = getLayoutInflater().inflate(R.layout.indicator, null); ImageView imageView = view.findViewById(R.id.iv_indicator); TextView textView = view.findViewById(R.id.tv_indicator); imageView.setImageResource(mImages[index]); textView.setText(mFragmentTags[index]); return view; }}
YFragment.java
public class YFragment extends Fragment { TextView mTvText; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, container, false); mTvText = view.findViewById(R.id.tab_tv_text); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTvText.setText(String.valueOf(getTag())); }}
3.3 图片资源
tab_daily.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/tab_daily_select"/> <item android:drawable="@drawable/tab_daily_unselect"/></selector>
其余几个tab标签的内容参照如上
4. 实际效果
5. 其他测试
5.1 操作一:注释掉 TabWidget
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <!--<TabWidget--> <!--android:id="@android:id/tabs"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="?attr/actionBarSize"--> <!--android:layout_gravity="bottom"/>--> </LinearLayout></android.support.v4.app.FragmentTabHost>
5.1 操作一:注释掉Id为@android:id/tabcontent的内容
<?xml version="1.0" encoding="utf-8"?><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"> <!--<FrameLayout--> <!--android:id="@android:id/tabcontent"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="0dp"--> <!--android:layout_weight="1"/>--> <!--<TabWidget--> <!--android:id="@android:id/tabs"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="?attr/actionBarSize"--> <!--android:layout_gravity="bottom"/>--> </LinearLayout></android.support.v4.app.FragmentTabHost>
6.备注
第五部分的解释可以从FragmentTabHost的源码中分析得到。
阅读全文
0 0
- <Android 基础(三十八)> FragmentTabHost
- Android Tab导航菜单栏--FragmentTabHost+Fragment实现(基础篇)
- javascript基础(键盘事件)(三十八)
- 八十、基础框架(三十八)复制 物件
- Java基础---方法进阶(三十八)
- Android FragmentTabHost
- android fragmenttabhost
- android FragmentTabHost
- Android FragmentTabHost嵌套(ViewPager+Fragment)问题
- Android (FragmentTabHost+RadioGroup)实现底部bar
- Android开发笔记(三十八)列表类视图
- Android开发笔记(一百三十八)文本输入布局TextInputLayout
- JavaScript 计时(三十八)
- 日拱一卒(三十八)
- (三十八)浮点类型
- 英语口语收集(三十八)
- (三十八)会话Session
- Android FragmentTabHost 示例
- 用shell脚本实现希尔排序
- Ubuntu16.04软件安装 Could not open file /var/lib/dpkg/lock
- L1-025. 正整数A+B
- Jenkins集成Cucumber生成图形化的测试报告
- $.fn.extend()和$.extend()的区别以及JQ插件开发
- <Android 基础(三十八)> FragmentTabHost
- OpenGL学习——入门篇 第二章 基础实例2-2
- 怎么解决java.lang.NoClassDefFoundError错误
- 少有人走的路个人理解
- 微型光学防抖摄像头模组的自动化生产调试技术
- Re:从零开始的Spring Security Oauth2(一)
- [笔试编程题] 腾讯2017暑期实习生编程题-java
- <a>标签的href和onclick属性
- log4j日志配置,约定大于配置