Android新浪微博开发(二)主UI的实现:动态建立TabActivity

仿照官网微博,设立5个窗口,这就需要TabActivity。动态建立TabActivity的步骤如下:

1 建立所需的Activity

跟官方的一样,也建5个,里面的先空着,别忘了最后要在配置文件中加上,并把启动的Activity改成下边的TabActivity

	<activity android:name="HomeTimeLineActivity" android:label="My Weibo">
		</activity>
		<activity android:name="infoactivity"></activity>
		<activity android:name="MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name="moreweiboactivity"></activity>
		<activity android:name="publicweiboactivity"></activity>
		<activity android:name="useractivity"></activity>

2 在res/layout中新建xml文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent">

	<FrameLayout android:id="@android:id/tabcontent"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		android:layout_weight="1.0" />

	<TabWidget android:id="@android:id/tabs"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:layout_alignParentBottom="true" android:layout_gravity="bottom"></TabWidget>
</TabHost>

要注意各个ID,都是固定的,TabHost ,TabWidget ,FrameLayout的ID必须分别为@android:id/tabhost,@android:id/tabs,@android:id/tabcontent

 

3 建立一个从TabActivity继承来的类

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity {

	private static final String WEIBO_HOME = "首页";
	private static final String WEIBO_INFO = "信息";
	private static final String WEIBO_USER = "我的资料";
	private static final String WEIBO_PUBLIC = "广场";
	private static final String WEIBO_MORE = "更多";
	
	private TabHost tabhost;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintab);
		
		tabhost = this.getTabHost();
		
		TabHost.TabSpec tspec;
		Intent it;
		
		//加上首页的Activity
		it = new Intent(MainActivity.this,HomeTimeLineActivity.class);
		tspec = tabhost.newTabSpec(WEIBO_HOME).setIndicator(WEIBO_HOME).setContent(it);
		tabhost.addTab(tspec);
		
		//加上信息页
		it = new Intent(MainActivity.this,infoactivity.class);
		tspec = tabhost.newTabSpec(WEIBO_INFO).setIndicator(WEIBO_INFO).setContent(it);
		tabhost.addTab(tspec);
		
		//加上我的资料
		it = new Intent(MainActivity.this,useractivity.class);
		tspec = tabhost.newTabSpec(WEIBO_USER).setIndicator(WEIBO_USER).setContent(it);
		tabhost.addTab(tspec);
		
		//加上广场
		it = new Intent(MainActivity.this,publicweiboactivity.class);
		tspec = tabhost.newTabSpec(WEIBO_PUBLIC).setIndicator(WEIBO_PUBLIC).setContent(it);
		tabhost.addTab(tspec);
		
		//加上更多
		it = new Intent(MainActivity.this,moreweiboactivity.class);
		tspec = tabhost.newTabSpec(WEIBO_MORE).setIndicator(WEIBO_MORE).setContent(it);
		tabhost.addTab(tspec);
		
		//设置当前的Activity
		tabhost.setCurrentTab(0);
	}
	

}

看看效果

Android新浪微博开发(二)主UI的实现:动态建立TabActivity_第1张图片

丑是丑了点,但基本效果是有的,谁让咱美工不行呢


你可能感兴趣的:(android,UI,新浪微博,String,layout,encoding)