Android TabHost的使用
1. 最簡單的TabHost,Tab來自于layout下的元素 (只從1個Layout中取數據)
(1)效果圖

(2)代碼
1)tab_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/tab_demo_tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab_demo_tv1"
/>
<TextView android:id="@+id/tab_demo_tv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab_demo_tv2"
/>
<TextView android:id="@+id/tab_demo_tv3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab_demo_tv3"
/>
</FrameLayout>
2)TabDemo.java
public class TabDemo extends TabActivity {
private TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab_demo, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", null).setContent(R.id.tab_demo_tv1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2", null).setContent(R.id.tab_demo_tv2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3", null).setContent(R.id.tab_demo_tv3));
setContentView(tabHost);
}
} </pre>
2. TabHost綁定動態View(從2個Layout中取數據)
(1)效果圖

(2)代碼
1)tab_map.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab_map_id"
>
</FrameLayout>
2)tab_hs.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab_hs_id"
>
<TextView android:id="@+id/tab_hs_tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
3)MapView.java
public class MapView extends View {
public MapView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), 0, 0, p);
}
}
4)TabDemo.java
public class TabDemo extends TabActivity {
private TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
createTabSpec_map();
createTabSpec_hs();
setContentView(tabHost);
}
private void createTabSpec_map() {
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab_map, tabHost.getTabContentView());
/*
* R.layout.tab_demo已被LayoutInflater注冊,所以這個可以通過findViewById獲得其對象
*/
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tab_map_id);
MapView mv = new MapView(this);
frameLayout.addView(mv);
TabHost.TabSpec tabSpec_map = tabHost.newTabSpec("map view");
tabSpec_map.setIndicator("map view", null);
tabSpec_map.setContent(R.id.tab_map_id); // 動態綁定基于圖片的View(通過一個Layout綁定)
tabHost.addTab(tabSpec_map);
}
private void createTabSpec_hs() {
LayoutInflater inflater_tab2 = LayoutInflater.from(this);
inflater_tab2.inflate(R.layout.tab_hs, tabHost.getTabContentView());
TabHost.TabSpec tabSpec_hs = tabHost.newTabSpec("hs view");
tabSpec_hs.setIndicator("hs view");
tabSpec_hs.setContent(R.id.tab_hs_id); // 綁定一個新的Layout
tabHost.addTab(tabSpec_hs);
/*
* 這個綁定View的操作必須要重新使用一個新方法來完成,因為Tab的生成是在onCreate()中完成的,onCreate()只被調用一次,
* 但是數據更新操作是需要反復進行的,如果反復調用createTabSpec_hs()則會生成多個tab頁,這是我們不希望的,所以單獨把
* updata分離出來,數據更新時單獨調用此方法就可以了。
*/
updateTabSpec_hs();
}
private void updateTabSpec_hs() {
TextView tv = (TextView) findViewById(R.id.tab_hs_tv);
tv.setText("This is tab2");
}
} </pre>
</div>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!