您的位置:首页 > 移动开发 > Android开发

android开发实例-TabHost的使用

2012-09-25 17:18 435 查看
布局文件要先添加<TabWidget/>(如红体所示);

一个个的小标题就是tab(清音、浊音、拗音);

盛放Tab的容器就是TabHost;

下面的framelayout布局中就可以插入tab了(如蓝体所示);

tab下面可以另外插入控件,我这里插入的是listview(如绿体所示);

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">

<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>

</LinearLayout>

<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>

<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

</RelativeLayout>


在java文件中定义的类为了方便使用getTabHost()函数可以继承TabActivity类;

package com.wxp.activity0922;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.support.v4.app.NavUtils;
import android.widget.*;

public class Activity1 extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);

TabHost tabHost=getTabHost();
Resources res=getResources();
TabHost.TabSpec spec;
Intent intent;
intent=new Intent().setClass(this,PingJia.class);
spec=tabHost.newTabSpec("***").setIndicator("***", res.getDrawable(R.drawable.qy)).setContent(intent);
tabHost.addTab(spec);

intent=new Intent().setClass(this,ZhuoYin.class);
spec=tabHost.newTabSpec("***").setIndicator("***", res.getDrawable(R.drawable.zy)).setContent(intent);
tabHost.addTab(spec);

intent=new Intent().setClass(this,AoYin.class);
spec=tabHost.newTabSpec("***").setIndicator("***", res.getDrawable(R.drawable.ay)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: