- Mar 22 Tue 2011 11:52
Android底部顯示TabHost 元件(附原始碼)

這範主是對TabHost元件的重新定義,實現標籤底部顯示,每個標籤的背景和高度,標籤文字的顏色、大小,標籤底部的白色橫線,每個標籤的分割線等屬性的修改。
1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tabhost" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTabHostFive" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Layout1" android:label="@string/app_name" />
<activity android:name=".Layout2" android:label="@string/app_name" />
<activity android:name=".Layout3" android:label="@string/app_name" />
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
2.MyTabHostFive.java
package com.android.tabhost;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
public class MyTabHostFive extends TabActivity
{
private TabHost mTabHost;
private TabWidget mTabWidget;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = this.getTabHost();
mTabHost.setPadding(mTabHost.getPaddingLeft(),
mTabHost.getPaddingTop(), mTabHost.getPaddingRight(),
mTabHost.getPaddingBottom() - 5);
Resources rs = getResources();
Intent layout1intent = new Intent();
layout1intent.setClass(this, Layout1.class);
TabHost.TabSpec layout1spec = mTabHost.newTabSpec("layout1");
layout1spec.setIndicator("layou1",
rs.getDrawable(android.R.drawable.stat_sys_phone_call));
layout1spec.setContent(layout1intent);
mTabHost.addTab(layout1spec);
Intent layout2intent = new Intent();
layout2intent.setClass(this, Layout2.class);
TabHost.TabSpec layout2spec = mTabHost.newTabSpec("layout2");
layout2spec.setIndicator("layout2",
rs.getDrawable(android.R.drawable.stat_sys_phone_call_forward));
layout2spec.setContent(layout2intent);
mTabHost.addTab(layout2spec);
Intent layout3intent = new Intent();
layout3intent.setClass(this, Layout3.class);
TabHost.TabSpec layout3spec = mTabHost.newTabSpec("layout3");
layout3spec.setIndicator("layout3",
rs.getDrawable(android.R.drawable.stat_sys_phone_call_on_hold));
layout3spec.setContent(layout3intent);
mTabHost.addTab(layout3spec);
mTabWidget = mTabHost.getTabWidget();
for (int i = 0; i < mTabWidget.getChildCount(); i++)
{
View view = mTabWidget.getChildAt(i);
if (mTabHost.getCurrentTab() == i)
{
view.setBackgroundDrawable(getResources().getDrawable(
R.drawable.number_bg_pressed));
}
else
{
view.setBackgroundDrawable(getResources().getDrawable(
R.drawable.number_bg));
}
mTabWidget.getChildAt(i).getLayoutParams().height = 50;
TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
android.R.id.title);
tv.setTextColor(Color.rgb(49, 116, 171));
}
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
for (int i = 0; i < mTabWidget.getChildCount(); i++)
{
View view = mTabWidget.getChildAt(i);
if (mTabHost.getCurrentTab() == i)
{
view.setBackgroundDrawable(getResources().getDrawable(
R.drawable.number_bg_pressed));
}
else
{
view.setBackgroundDrawable(getResources().getDrawable(
R.drawable.number_bg));
}
}
}
});
}
}
3.Layout1.java
package com.android.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Layout1 extends Activity
{
private TextView textView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
textView = (TextView) findViewById(R.id.text1);
textView.setText("我是第一頁");
}
}
4.Layout2.java
package com.android.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Layout2 extends Activity
{
private TextView textView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
textView = (TextView) findViewById(R.id.text2);
textView.setText("我是第二頁");
}
}
5.Layout3.java
package com.android.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Layout3 extends Activity
{
private TextView textView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout3);
textView = (TextView) findViewById(R.id.text3);
textView.setText("我是第三頁");
}
}
6.執行結果:

原始碼下載:http://www.fileserve.com/file/5PEXzbV
感謝網路上的這篇文章使我能夠順利的引用書籤的功能
成功將按鈕與socket訊號控制建構在一起



文章出處:
http://chiacheng.pixnet.net/blog/trackback/0c6550ed43/50954926





































windows 會為了省電而將顯示卡的電源減少,因此導致 fps 減少
跟大家分享~


















