TabHost 选项卡

就是常见这个界面,可以选择的页卡:

 

public class MyTabHostAndGridViewActivity extends TabActivity {
	private ImageView mImageTabHost;
	private TextView mTextTabHost;
	private LayoutInflater mLayoutInflater;
	private TabHost mTabHost;
	final private int one = 1;
	final private int two = 2;
	final private int three = 3;
	final private int four = 4;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_tabhost_layout);
		mLayoutInflater = LayoutInflater.from(this);

		
		//实例化TabHost
		mTabHost = getTabHost();
		//创建第一个页卡
		TabSpec spe1 = mTabHost.newTabSpec("One");
		spe1.setIndicator(getListView(one));
		spe1.setContent(new Intent(this, MyTabOneGostActivity.class));
		//创建第二个页卡
		TabSpec spe2 = mTabHost.newTabSpec("Two");
		spe2.setIndicator(getListView(two));
		spe2.setContent(new Intent(this, MainActivity.class));
		//创建第三个页卡
		TabSpec spe3 = mTabHost.newTabSpec("Three");
		spe3.setIndicator(getListView(three));
		spe3.setContent(new Intent(this, MyTabWidgetActivity.class));
		//创建第四个页卡
		TabSpec spe4 = mTabHost.newTabSpec("Four");
		spe4.setIndicator(getListView(four));
		spe4.setContent(new Intent(this, MyMoreActivity.class));
		然后在把创建的页卡加入到TabWidget中	
		mTabHost.addTab(spe1);
		mTabHost.addTab(spe2);
		mTabHost.addTab(spe3);
		mTabHost.addTab(spe4);

	}
	//自定义页卡的名字
	public View getListView(int n) {

		View viewTab = mLayoutInflater.inflate(
				R.layout.activity_main_tabhost_public_layout, null);
		mImageTabHost = (ImageView) viewTab
				.findViewById(R.id.main_tab_host_iamge);
		mTextTabHost = (TextView) viewTab
				.findViewById(R.id.main_tab_host_text_content);

		switch (n) {
		case one:
			mImageTabHost.setBackgroundResource(R.drawable.select_my);
			mTextTabHost.setText("团购");
			break;
		case two:
			mImageTabHost.setBackgroundResource(R.drawable.selcet_business);
			mTextTabHost.setText("商家");
			break;
		case three:
			mImageTabHost.setBackgroundResource(R.drawable.select_me);
			mTextTabHost.setText("我的");
			break;
		case four:
			mImageTabHost.setBackgroundResource(R.drawable.select_more);
			mTextTabHost.setText("更多");
			break;
		}
		return viewTab;
	}
}
代码的布局文件:均需要引用系统的ID来处理
 
<?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="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/bung"
            android:paddingBottom="5dp"
            android:paddingTop="5dp" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true" 
            android:layout_above="@android:id/tabs">
        </FrameLayout>
    </RelativeLayout>

</TabHost>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值