下面和大家分享一個實際工作中經常使用的TabWidget標簽分頁技術,先看一下效果:
下面和大家分享一下實現代碼:
MainActivity.java
package com.zyb.tab;
import Android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
public class MainActivity extends TabActivity {
//分頁容器
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost=getTabHost();
//新建一個標簽
TabHost.TabSpec spec1=tabHost.newTabSpec("tab1");
//設置第一個標簽的標題和圖標
spec1.setIndicator("第一個標簽", getResources().getDrawable(android.R.drawable.arrow_down_float));
//設置第一個標簽的內容為FirstTabActivity
spec1.setContent(new Intent(this,FirstTabActivity.class));
//定義第二個標簽
TabHost.TabSpec spec2=tabHost.newTabSpec("tab2").setIndicator("第二個標簽",
getResources().getDrawable(android.R.drawable.arrow_up_float)).setContent(new Intent(this,SecondTabActivity.class));
//定義第三個標簽
TabHost.TabSpec spec3=tabHost.newTabSpec("tab3").setIndicator("第三個標簽",
getResources().getDrawable(android.R.drawable.ic_delete)).setContent(new Intent(this,ThirdTabActivity.class));
//把標簽添加到容器中
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
//為TabHost添加事件
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("tab1")){
Toast.makeText(MainActivity.this, "選擇了第一個標簽", Toast.LENGTH_SHORT).show();
}else if(tabId.equals("tab2")){
Toast.makeText(MainActivity.this, "選擇了第二個標簽", Toast.LENGTH_SHORT).show();
}else if(tabId.equals("tab3")){
Toast.makeText(MainActivity.this, "選擇了第三個標簽", Toast.LENGTH_SHORT).show();
}
}
});
}
}
FirstTabActivity.java
package com.zyb.tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
/**
* 第一個標簽的內容
* @author Administrator
*
*/
public class FirstTabActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setText("這是第一個標簽的內容");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
setContentView(tv);
}
}
SecondTabActivity.java
package com.zyb.tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
/**
* 第二個標簽的內容
* @author Administrator
*
*/
public class SecondTabActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setText("這是第二個標簽的內容");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
setContentView(tv);
}
}
ThirdTabActivity.java
package com.zyb.tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
/**
* 第三個標簽的內容
* @author Administrator
*
*/
public class ThirdTabActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setText("這是第三個標簽的內容");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
setContentView(tv);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android: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>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zyb.tab"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".FirstTabActivity"></activity>
<activity android:name=".SecondTabActivity"></activity>
<activity android:name=".ThirdTabActivity"></activity>
</application>
</manifest>
OK,大功就此告成。