Android实例 - TabHost

今天我们来写一个TabHost的实例,先来一张效果图:


主要代码:

1. res/ layout/ activity_main.xml

<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"
        android:orientation="vertical" >
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>
        
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
        </TabWidget>
    </RelativeLayout>


</TabHost>

2. src/ MainActivity.java

package com.example.demo_ui7_tabwdget;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
	private Context ctx = this;
	private TabHost tabhost;
	private Intent intentStudy;
	private Intent intentScan;
	private Intent intentReview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intentStudy = new Intent(MainActivity.this, StudyActivity.class);
		intentScan = new Intent(MainActivity.this, ScanActivity.class);
		intentReview = new Intent(MainActivity.this, ReviewActivity.class);
		tabhost = getTabHost();
		tabhost.addTab(tabhost
				.newTabSpec("tab1")
				.setIndicator("Tab1",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(intentStudy));
		tabhost.addTab(tabhost
				.newTabSpec("tab2")
				.setIndicator("Tab2",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(intentScan));
		tabhost.addTab(tabhost
				.newTabSpec("tab3")
				.setIndicator("Tab3",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(intentReview));
		tabhost.setCurrentTab(0);
		tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				Toast.makeText(ctx, ""+tabId, Toast.LENGTH_SHORT).show();
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

这里只贴出了MainActivity和activity_main布局文件, 宁外用到了 StudyActivity, ScanActivity, ReviewActivity 就是三个普通的activity,这里就不贴代码了,请大家自行添加。

我们在使用TabHost的时候一定要注意,一个应用中只能有一个TabHost组件,并且其获取方式也和其他的组件不同。我们来看一看:

tabhost = getTabHost(); //直接通过系统得到

添加tab的方式:

tabhost.addTab(tabhost
    .newTabSpec("tab1")
    .setIndicator("Tab1",getResources().getDrawable(R.drawable.ic_launcher))
    .setContent(intentStudy));
这里我把它整理一下,这样好看一点。

newTabSpec("tab1") :"tab1"将作为tabId

setIndicator(“Tab1", getResource().getDrawable(R.drawable.ic_launcher) :设置tab的标题,以及icon(但是这里好像icon没有起作用)

setContent( intent ) : 这里传入 intent对象 ,说明切换tab的时候,显示的是intent对象所跳转到的activity。


然后这里在写layout的时候有几点一定要注意:

TabHost的id, FramLayout的id, TabWidget的id:

android:id="@android:id/tabhost"
android:id="@android:id/tabcontent"
android:id="@android:id/tabs"
这三个id的写法和普通的不一样: android:id="@+id/ ..." (普通)。

如果你在写实例的时候发现app运行错误,报 tabhost找不到, tabcontent找不到, 或tabs找不到,那么多半是这里出了问题。


好了,实例写到这里,完!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值