---
title: "【转】Android 分页控件制成底部菜单"
author: "que2010"
date: "2012-01-16T17:47:00Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-01-16T17:47:00Z"
id: 1350892
url: "https://que2010.iteye.com/blog/1350892"
---

转自：[http://blog.csdn.net/knowheart/article/details/7049596](http://blog.csdn.net/knowheart/article/details/7049596)

其实Android 中的底部菜单， 可以用分页控件很好的实现。   我们先将自定义分页控件做好， 就可以做到顶底两个位置的菜单了。

 

  TabHost只是作为一个容器来存放一些Activity, 所以需要自己另外创建几个新的Activity， 然后由主TabHost加载。

  ![](http://hi.csdn.net/attachment/201112/7/0_1323238279BTbb.gif)

 

tab\_style.xml  

是每个Tab的自定义样式

```
//分页控件样式
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:paddingLeft="5dip"
	android:paddingRight="5dip"
	android:paddingTop="5dip"
	android:background="@drawable/tab_bg"
	>
	
	<FrameLayout 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:layout_weight="0.6"
		>
		<TextView 
			android:id="@+id/tab_label"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:gravity="center"
			android:background="@drawable/tab_title_selector"
			android:textColor="#FFFFFF"
			android:textStyle="bold"
		/>
	</FrameLayout>	
</LinearLayout>
```

 

 

main\_tab.xml   是主TabHost布局文件

```
//TabHost布局
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
	
 	//必须包含下列三个View
	<LinearLayout
		android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		>

<FrameLayout 
			android:gravity="center" 
			android:id="@android:id/tabcontent"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:layout_weight="1.0"
		/>
		
		//TabWidget位置在FrameLayout之下则显示在低部， 在之上则显示在顶部
		<TabWidget 
			android:id="@android:id/tabs"
			android:layout_height="wrap_content"
			android:layout_width="fill_parent"
			android:layout_weight="0.0"
			/>
		
	</LinearLayout>	
	
</TabHost>
```

 

 

tab\_title\_selector.xml

是Tab中TextView的按下背景

```
//选择器，指示Text按下后的背景
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:state_focused="true"
		android:drawable="@drawable/tab_btn_bg_d"
		/>
	<item
		android:state_selected="true"
		android:drawable="@drawable/tab_btn_bg_d"
		/>
	<item
		android:state_pressed="true"
		android:drawable="@drawable/tab_btn_bg_d"
		/>
</selector>
```

 

 

Activity类

另外还需要几个Activity类， 普通的Activity类即可, 在此不显示。

```
public class TabTest extends TabActivity
{
	private TabWidget mTabWidget;
	private TabHost	mTabHost;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tabs);
		
		mTabHost = getTabHost();
		
		//将要显示的Activity载入TabHost控件
		//要显示的Activity由自己自由创建
		setTabIndicator("one", 1, new Intent(this, OneActivity.class));
		setTabIndicator("Two", 2, new Intent(this, TwoActivity.class));
		setTabIndicator("Three", 3, new Intent(this, OneActivity.class));
		setTabIndicator("Four", 4, new Intent(this, TwoActivity.class));
	}
	
	private void setTabIndicator(String title, int nId, Intent intent)
	{
		//使用指定Tab样式
		View view = LayoutInflater.from(this.mTabHost.getContext())
					.inflate(R.layout.tab_style, null);
		
		TextView text 	= (TextView)view.findViewById(R.id.tab_label);
		String strId 	= String.valueOf(nId);
		
		text.setText(title);
		
		//创建一个新Tab
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(strId)
						.setIndicator(view).setContent(intent);
		//加载新Tab
		mTabHost.addTab(localTabSpec);
	}
}
```