本文結(jié)合源代碼和實例來說明TabHost的用法。
使用TabHost 可以在一個屏幕間進(jìn)行不同版面的切換,例如android自帶的撥號應(yīng)用,截圖:
查看tabhost的源代碼,主要實例變量有:
private TabWidget mTabWidget;
private FrameLayout mTabContent;
private List<TabSpec> mTabSpecs
也就是說我們的tabhost必須有這三個東西,所以我們的.xml文件就會有規(guī)定:繼續(xù)查看源代碼:
if (mTabWidget == null) {
throw new RuntimeException(
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}
mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
if (mTabContent == null) {
throw new RuntimeException(
"Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
}
也就是說我們的.xml文件需要TabWidget和FrameLayout標(biāo)簽。
接下來構(gòu)建我們自己的tab實例:
有兩種方式可以實現(xiàn):
一種是繼承TabActivity 類,可以使用android的自己內(nèi)部定義好的.xml資源文件作容器文件。也就是在我們的代碼中使用getTabHost(); , 而相應(yīng)的后臺源碼是這樣的:
this.setContentView(com.android.internal.R.layout.tab_content);
在系統(tǒng)的資源文件中可以看見這個layout
有了容器,然后我們就需要我們?yōu)槊總€tab分配內(nèi)容,當(dāng)然要可以是如何類型的標(biāo)簽:
例如我們構(gòu)建一下.xml文件
首先tab1.xml 是一個LinearLayout布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="tab1 with linear layout"
android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
然后是tab2.xml是一個FrameLayout布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="tab2"
android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</FrameLayout>
接著要注冊這兩個FrameLayout為tabhost的Content,也就是接下來的代碼:
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
然后需要構(gòu)建前面說的tabhost的第三個實例變量對應(yīng)得內(nèi)容,源代碼中是這樣的:
private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
初始化是兩個tab的空間然后會自動擴(kuò)展:
好 我們構(gòu)建我們的tabspec:
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.FrameLayout02));
也就是把我們的2個layout作為他的content,當(dāng)然FrameLayout中可以有其他的布局,來放我的組件。
我們不需要在代碼里面設(shè)置setContentView();因為getTabHost(); 這個方法調(diào)用后就已經(jīng)設(shè)置了,源代碼:
if (mTabHost == null) {
this.setContentView(com.android.internal.R.layout.tab_content);
}
也就是把系統(tǒng)的tab_content當(dāng)做view設(shè)置。
運(yùn)行后如下:
完整代碼:
TabHost mTabHost = getTabHost();
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.FrameLayout02));
還有一種就是定義我們自己的tabhost:不用繼承TabActivity
首先建立我們自己的.xml文件,當(dāng)然要包含Tabhost,TabWidget,FrameLayout,著3個標(biāo)簽:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
注意的是:除了tabhost的id可以自定義外,其他的必須使用系統(tǒng)的id,為什么后面說,
當(dāng)然我們可以在FrameLayout里面添加view來作為tab的內(nèi)容只需要在create tabspce時候添加就可以了,我們?yōu)榱税衙總€tab的內(nèi)容分開我們依然使用前面用到的兩個tab xml文件
java代碼:
獲取TabHost 通過findviewbyid,
setContentView(R.layout.main);
TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
接下來很重要的一步是要使用TabHost.setup();
作用是來初始化我們的TabHost容器:
源代碼是這樣說的:
<p>Call setup() before adding tabs if loading TabHost using findViewById(). <i><b>However</i></b>: You do
* not need to call setup() after getTabHost() in {@link android.app.TabActivity TabActivity}.
也就是說通過findviewbyid,方法獲得tabhost必須setup 而通過getTabHost則不用。
setup干什么呢:源代碼
mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
if (mTabWidget == null) {
throw new RuntimeException(
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}
mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
if (mTabContent == null) {
throw new RuntimeException(
"Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
}
他主要是初始化了tabhost的兩個實例變量,這里也回答了為什么我們的id必須使用系統(tǒng)定義的id的原因
接下來工作就和前面相同了:
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.FrameLayout02));
完整代碼:
setContentView(R.layout.main);
TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.FrameLayout02));
運(yùn)行結(jié)果同上。 如有問題歡迎提出。
轉(zhuǎn)載請說明出處。。。
加上源代碼,有用了可以下載下:/Files/freeman1984/atab.rar
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/lastsweetop/archive/2010/05/07/5566200.aspx
package com.TabTest2;
import java.lang.reflect.Field;
import android.app.Activity;
import android.os.Build;
import android.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
public class TabTest2 extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(com.TabTest2.R.layout.main);
final TabHost tabs=(TabHost)findViewById(com.TabTest2.R.id.tabhost);
final TabWidget tabWidget=(TabWidget) findViewById(R.id.tabs);
int width =45;
int height =48;
Field mBottomLeftStrip;
Field mBottomRightStrip;
tabs.setup();
tabs.addTab(tabs.newTabSpec("first tab")
.setIndicator("news",getResources().getDrawable(com.TabTest2.R.drawable.c))
.setContent(com.TabTest2.R.id.tab1));
tabs.addTab(tabs.newTabSpec("second tab")
.setIndicator("help",getResources().getDrawable(com.TabTest2.R.drawable.e))
.setContent(com.TabTest2.R.id.tab2));
tabs.addTab(tabs.newTabSpec("third tab")
.setIndicator("setting",getResources().getDrawable(com.TabTest2.R.drawable.efg))
.setContent(com.TabTest2.R.id.tab2));
tabs.setCurrentTab(0);
for (int i =0; i <tabWidget.getChildCount(); i++) {
/**
* 設(shè)置高度、寬度,不過寬度由于設(shè)置為fill_parent,在此對它沒效果
*/
tabWidget.getChildAt(i).getLayoutParams().height = height;
tabWidget.getChildAt(i).getLayoutParams().width = width;
/**
* 設(shè)置tab中標(biāo)題文字的顏色,不然默認(rèn)為黑色
*/
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
/**
* 此方法是為了去掉系統(tǒng)默認(rèn)的色白的底角
*
* 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
* 都是私有變量,但是我們可以通過反射來獲取
*
* 由于還不知道Android 2.2的接口是怎么樣的,現(xiàn)在先加個判斷好一些
*/
if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
try {
mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
if(!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if(!mBottomRightStrip.isAccessible()){
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tabWidget, getResources().getDrawable (com.TabTest2.R.drawable.linee));
mBottomRightStrip.set(tabWidget, getResources().getDrawable (com.TabTest2.R.drawable.linee));
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
View vvv = tabWidget.getChildAt(i);
if(tabs.getCurrentTab()==i){
vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.shine));
}
else {
vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.seven));
}
}
/**
* 當(dāng)點(diǎn)擊tab選項卡的時候,更改當(dāng)前的背景
*/
tabs.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i =0; i < tabWidget.getChildCount(); i++) {
View vvv = tabWidget.getChildAt(i);
if(tabs.getCurrentTab()==i){
vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.shine));
}
else {
vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.seven));
}
}
}});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@+id/tabhost"
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:paddingBottom="62px">
<AnalogClock
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button" />
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="60px" />
</RelativeLayout>
</TabHost>
</LinearLayout>
package com.testTabActivity;
import java.lang.reflect.Field;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
public class testTabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int width =45;
int height =48;
final TabHost tabs = getTabHost();
final TabWidget tabWidget = tabs.getTabWidget();
Field mBottomLeftStrip;
Field mBottomRightStrip;
LayoutInflater.from(this).inflate(R.layout.main, tabs.getTabContentView(), true);
tabs.addTab(tabs.newTabSpec("first tab")
.setIndicator("news",getResources().getDrawable(R.drawable.efg))
.setContent(R.id.widget_layout_Blue));
tabs.addTab(tabs.newTabSpec("second tab")
.setIndicator("help",getResources().getDrawable(R.drawable.c))
.setContent(R.id.widget_layout_green));
tabs.addTab(tabs.newTabSpec("second tab")
.setIndicator("setting",getResources().getDrawable(R.drawable.e))
.setContent(R.id.widget_layout_red));
for (int i =0; i < tabWidget.getChildCount(); i++) {
/**
* 設(shè)置高度、寬度,不過寬度由于設(shè)置為fill_parent,在此對它沒效果
*/
tabWidget.getChildAt(i).getLayoutParams().height = height;
tabWidget.getChildAt(i).getLayoutParams().width = width;
/**
* 設(shè)置tab中標(biāo)題文字的顏色,不然默認(rèn)為黑色
*/
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
/**
* 此方法是為了去掉系統(tǒng)默認(rèn)的色白的底角
*
* 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
* 都是私有變量,但是我們可以通過反射來獲取
*
* 由于還不知道Android 2.2的接口是怎么樣的,現(xiàn)在先加個判斷好一些
*/
if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
try {
mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
if(!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if(!mBottomRightStrip.isAccessible()){
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));
mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
View vvv = tabWidget.getChildAt(i);
if(tabs.getCurrentTab()==i){
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.shine));
}
else {
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.seven));
}
}
/**
* 當(dāng)點(diǎn)擊tab選項卡的時候,更改當(dāng)前的背景
*/
tabs.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i =0; i < tabWidget.getChildCount(); i++) {
View vvv = tabWidget.getChildAt(i);
if(tabs.getCurrentTab()==i){
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.shine));
}
else {
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.seven));
}
}
}});
}
}
類似ASP的<%,PHP可以是<?php或者是<?,結(jié)束符號是?>,當(dāng)然您也可以自己指定。通常情況下,有以下幾種方式:
(1) <?php…?> //推薦使用
(2) <?...?>
(3) <script language=“php” >…</script>
(4) <%…%>
PHP變量名的約定:
(1) PHP的變量名區(qū)分大小寫;
(2) 變量名必須以美元符號$開始;
(3) 變量名開頭可以以下劃線開始;
(4) 變量名不能以數(shù)字字符開頭.
單引號
指定一個簡單字符串的最簡單的方法是用單引號(字符 ')括起來。
注: 單引號字符串中出現(xiàn)的變量不會被變量的值替代。
雙引號
如果用雙引號(")括起字符串,PHP 懂得更多特殊字符的轉(zhuǎn)義序列:
雙引號字符串最重要的一點(diǎn)是其中的變量名會被變量值替代。
<?php
$name = “Jane”;
print(“your name is $name”);
?>
以上腳本輸出 your name is Jane
在PHP中,字符串內(nèi)可以任意插入變量。
相關(guān)函數(shù) is_set():判斷變量是否設(shè)置 empty():判斷變量是否為空 var_dump():判斷變量類型 is函數(shù):例如is_int(),判斷變量是否是指定類型
<?php
$a="";
$b=0;
$c=5;
$d="hello";
if(isset($a))
echo "\$a已經(jīng)定義<br>";
else
echo "\$a未定義<br>";
if(empty($b))
echo "\$b不為空<br>";
else
echo "\$b為空<br>";
echo "\$c類型是:";
var_dump($c);
echo "<br>\$d類型是:";
var_dump($d);
if(is_numeric($c))
echo "<br>\$c是整型";
else
echo "<br>\$c不是整型<br>";
?>
Floats :
<?php$var1 = 3.14?>Floating point: <?php echo $myFloat = 3.14; ?>Round: <?php echo round($myFloat, 1); ?>Ceiling: <?php echo ceil($myFloat); ?>Floor: <?php echo floor($myFloat); ?>
輸出結(jié)果:
Floating point: 3.14
Round: 3.1
Ceiling: 4
Floor: 3
Booleans:<?php
$bool1 = true;
$bool2 = false;
?>
$bool1: <?php echo $bool1; ?><br />
$bool2: <?php echo $bool2; ?><br />
<br />
<?php
$var1 = 3;
$var2 = "cat";
?>
$var1 is set: <?php echo isset($var1); ?><br />
$var2 is set: <?php echo isset($var2); ?><br />
$var3 is set: <?php echo isset($var3); ?><br />
輸出結(jié)果:
$bool1: 1
$bool2:
$var1 is set: 1
$var2 is set: 1
$var3 is set:
Typecasting:
<?php
$var1 = "2 ";
$var2 = $var1 + 3;
echo $var2;
?>
<br />
<?php
echo gettype($var1); echo "<br />";
echo gettype($var2); echo "<br />";
settype($var2, "string");
echo gettype($var2); echo "<br />";
$var3 = (int) $var1;
echo gettype($var3); echo "<br />";
?>
輸出結(jié)果:
5
string
integer
string
integer