標籤:
註:本文為轉載,但該內容本人已親身嘗試,確認該方法可行,代碼有點小的改動,轉載用作儲存與分享。
原作者地址:http://gundumw100.iteye.com/blog/853967
個人吐嘈:據說TabWidget已經過時了,取而代之的是Fragment,但關於這個Fragment暫時還沒時間研討,現使用的是TabWidget方法,個人感覺實現出來的效果還是很不錯的。
自訂TabHost:不用繼承TabActivity,具體代碼如下:
定義布局檔案:activity_main.xml
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TabHost01" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" 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"> <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="two" android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ScrollView android:id="@+id/Scroll01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" android:background="@android:color/white" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="2dp"> <TextView android:id="@+id/title01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="20sp" android:text="員工資訊查詢測試" android:padding="5dp"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="4dp" android:textSize="20sp" android:text="測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n測試\n" android:layout_weight="1"/> </LinearLayout> </ScrollView> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="three" android:id="@+id/TextView03" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> </FrameLayout> </LinearLayout></TabHost>
編寫Java代碼:
package com.example.test2;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.widget.TabHost;public class MainActivity extends Activity { /** Called when the activity is first created. */ private TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try{ tabHost = (TabHost) this.findViewById(R.id.TabHost01); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab_1") .setContent(R.id.LinearLayout1) //.setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img01))); .setIndicator("TAB1")); tabHost.addTab(tabHost.newTabSpec("tab_2") .setContent(R.id.LinearLayout2) //.setIndicator("TAB2",this.getResources().getDrawable(R.drawable.img01))); .setIndicator("TAB2")); tabHost.addTab(tabHost.newTabSpec("tab_3") .setContent(R.id.LinearLayout3) //.setIndicator("TAB3",this.getResources().getDrawable(R.drawable.img01))); .setIndicator("TAB3")); tabHost.setCurrentTab(1); }catch(Exception ex){ ex.printStackTrace(); Log.d("EXCEPTION", ex.getMessage()); } } }
運行圖:
本人提供Demo下載:點擊此處下載Demo
-全文完-
Android 實現分頁(使用TabWidget/TabHost)