众所周知,ActivityGroup和TabHost已经过时,除了使用FragmentTabHost外,我们可以简单的来实现一个界面丰富的底端菜单导航栏。
先不废话,上图:
其中,图片是反编译的淘宝客户端。手上没有合适的图片,凑合看吧。文章最后我会贴出我的源代码链接(因为我没有找到可以直接上传文件),里面有很多关键注释,文章没有写的地方,大家可以在代码中找到。
一.使用的知识:fragment +TabHost
二.布局文件:(最主要的文件),上代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sdf.MainActivity$PlaceholderFragment" >
<TabHost
android:id="@+id/tabhost"
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="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<fragment
android:id="@+id/content1"
android:name="com.lihuan.intelligencehumansocial.fragment.FirstFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/content2"
android:name="com.lihuan.intelligencehumansocial.fragment.SecondFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/content3"
android:name="com.lihuan.intelligencehumansocial.fragment.FirstFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<fragment
android:id="@+id/content4"
android:name="com.lihuan.intelligencehumansocial.fragment.SecondFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/content5"
android:name="com.lihuan.intelligencehumansocial.fragment.FirstFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
注意事项:1.fragment作为内容填充,要在fragment类中实现onCreateVIew方法。
2.TabWidget的id不能乱改,只能是那个
3.类文件:
public class TabHostActivity extends FragmentActivity {
// FragmentActivity和ActivityGroup继承父类相同。TabActivity继承ActivityGroup,两个都已经过期不用
// 使用FragmentActivity来代替。
private TabHost tabHost;
private TabWidget tabWiget;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
this.setContentView(R.layout.activity_main);
View guideHome = (View) LayoutInflater.from(this).inflate(
R.layout.guide_home, null);
View guideWeiTao = (View) LayoutInflater.from(this).inflate(
R.layout.guide_weitao, null);
View guideLife = (View) LayoutInflater.from(this).inflate(
R.layout.guide_nearby, null);
View guideShop = (View) LayoutInflater.from(this).inflate(
R.layout.guide_shop, null);
View guideAccount = (View) LayoutInflater.from(this).inflate(
R.layout.guide_account, null);
// 获取tabHost,tabHost是个容器组件
tabHost = (TabHost) findViewById(R.id.tabhost);
// Call setup() before adding tabs if loading TabHost using
// findViewById() except TabActivity
tabHost.setup(); // 类似于初始化过程,不调用无法获取到tabWiget,调用addTab需要tabwiget。TabActivity内部已经调用了
TabSpec mTabSpec1 = tabHost.newTabSpec("tab1").setIndicator(guideHome)
.setContent(R.id.content1);
TabSpec mTabSpec2 = tabHost.newTabSpec("tab2")
.setIndicator(guideWeiTao).setContent(R.id.content2);
TabSpec mTabSpec3 = tabHost.newTabSpec("tab3").setIndicator(guideLife)
.setContent(R.id.content3);
TabSpec mTabSpec4 = tabHost.newTabSpec("tab4").setIndicator(guideShop)
.setContent(R.id.content4);
TabSpec mTabSpec5 = tabHost.newTabSpec("tab5")
.setIndicator(guideAccount).setContent(R.id.content5);
tabHost.addTab(mTabSpec1);
tabHost.addTab(mTabSpec2);
tabHost.addTab(mTabSpec3);
tabHost.addTab(mTabSpec4);
tabHost.addTab(mTabSpec5);
// 获取TabWiget ,布局文件中一定要注意id=tabs,因为TabHost类中,tabWeiget获取是通过id=tabs
tabWiget = tabHost.getTabWidget(); // tabWiget的作用,设置tabWiget的总体的布局。
/**
* 可以对tabWiget进行布局设置
*/
}
注意事项:1.请继承FragmentActivity;2. tabHost = (TabHost) findViewById(R.id.tabhost);后请立即调用setup()。setup()中,初始化了各种数据,如果不调用,后面的操作将出现异常,大家可以查看setup()源代码。
希望大家再看完我的文章后,能够避免很多错误,高效率的做出精美的底部菜单导航栏。
附源代码:http://download.csdn.net/detail/meijian531161724/7527103
史上最详细的TabHost菜单栏实现
最新推荐文章于 2021-05-26 17:36:16 发布