首先吐槽下官网,http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html ,这里居然没给出xml的配置,导致一直弄了很久。而百度搜索到的大部分都是这个配置,不只是否是版本升级的原因,我按照这个配置,运行就会报错:java.lang.RuntimeException: Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'
各种尝试,最后发现只有下面这个代码能运行,但是tabs是在顶部的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
最后只好谷歌了,发现了国外的大神的一个配置,就是FragmentTabHost里面什么都没放置。下面是我自己写的配置,
<?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" > <!-- 把FragmentLayout放在FragmentTabHost上面,这样tabs就在底部了,注意,id要自己添加了 android:id="@+id/realtabcontent" --> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="150px" > </android.support.v4.app.FragmentTabHost> </LinearLayout>
下面是MainActivity.java的代码
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.View; import android.widget.LinearLayout; import com.dzc.talkv3.R; import com.dzc.talkv3.fragment.ContactFragment; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); setContentView(R.layout.activity_main1); //初始化tabs initTabs(); } private void initTabs(){ mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); //初始化4个tabs界面 //R.layout.main_bottom_bar_tab_msg 自己定义的tabs布局文件 一个Linearlayout里面放一个View组件就行了 View tab_msg = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_msg, null); View tab_contact = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_contact, null); View tab_plugin = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_plugin, null); View tab_me = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_me, null); //addTab(标题,跳转的Fragment,传递参数的Bundle) //ContactFragment自己定义一个extends Fragment的类就行了 mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_msg), ContactFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_contact), ContactFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_plugin), ContactFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_me), ContactFragment.class, null); //设置tabs之间的分隔线不显示 mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); } } 这是效果图
<!--StartFragment -->
相关推荐
ActionBar取代了传统的标题栏(title),其在activity位置就是title原有... ② 为在多个fragments之间切换提供标签(tabs)功能; ③ 为导航提供下拉列表; ④ 在“action项”位置提供交互式action组件,例如搜索框。
Android CustomTabs Chrome CustomTabs for Android demystified. Simplifies development and provides higher level classes including fallback in case Chrome isn't available on device. Usage How to ...
Android tabs at bottom! This repository contains sample of setting android TabLayout at bottom and showing Fragment embedded in ViewPager. The magic happens very simply. You need to arrange them ...
博客地址:http://blog.csdn.net/tyzlmjj/article/details/47904609
代码比较简洁,ScrollView+ViewPager实现tabs布局。
主要介绍了Android基于API的Tabs3实现仿优酷tabhost效果,结合完整实例形式分析了Android实现优酷界面效果的相关技巧,需要的朋友可以参考下
Android Studio 使用BottomNavigationView 实现底部 tabs (一)
Android Studio 使用BottomNavigationView 实现底部 tabs (二)
Tabby (Cat), is a simple sample application demoing the features of the new Chrome Custom Tabs support library for Android. Using this application you can demo: Coloring the toolbar Displaying the ...
A clone of ActionBarTabs in Android but is compatible with Toolbar You may get this library by adding the following in the dependency section of your build.gradle file: compile '...
android-customtabs:神秘的Android版Chrome CustomTabs。 简化开发并提供更高级别的类,包括万一Chrome无法在设备上使用的情况下的回退
界面原型、tabs选项卡增加效果 界面原型、tabs选项卡增加效果 界面原型、tabs选项卡增加效果 界面原型、tabs选项卡增加效果
EasyTabs让您可以轻松实现自定义选项卡控件
EasyTabs Easy implementation of custom tabs, when TabLayout do not fit your needs. With Textviews : Or Drawables : Installation Add the jitpack source in your root build.gradle at the end of ...
Ionic是一款流行的移动端开发框架,但是刚入门的同学会发现,Ionic在iOS和Android的底部tabs显示不一样。在安卓情况下底部tabs会浮上去,下面给大家介绍下实现代码,一起看看吧
引用: https://blog.csdn.net/qq_34906385/article/details/93524163 app:tabIndicatorColor :指示线的颜色 app:tabIndicatorHeight : 指示线的高度 app:tabIndicatorFullWidth=false 指示线是否铺满宽度 app:...
android 学习demo customized-tabs
这是一款基于bootstrap的选项卡。该选项卡的tab被设计在底部,通过简单的CSS代码来对其进行美化,效果非常不错。