標籤:
第一步,布局檔案
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabhost" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/top"/> <TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs" ></TabWidget> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@android:id/tabcontent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lkgj" android:background="#ffffff" android:orientation="vertical" android:layout_gravity="center" > <TextView android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_gravity="center" android:textSize="20dp" android:text="@string/gjtext" /> <EditText android:layout_width="280dp" android:layout_height="40dp" android:id="@+id/lkgjIpEdittext" android:layout_marginTop="10dp" android:layout_gravity="center" android:singleLine="true" android:hint="請輸入IP地址" android:textSize="15dip" android:textColorHint="#AAAAAA" android:layout_margin="20dip" android:padding="5dip" android:background="@drawable/shape" android:inputType="text" /> <Button android:id="@+id/lkgjBtn" android:layout_width="250dp" android:layout_height="80dp" android:layout_gravity="center" android:layout_marginTop="40dp" android:text="@string/lkgjbtn" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dsgj" android:background="#ffffff" android:orientation="vertical" > <TextView android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_gravity="center" android:textSize="20dp" android:text="@string/gjtext" /> <EditText android:layout_width="280dp" android:layout_height="40dp" android:id="@+id/dsgjEdittext" android:layout_marginTop="10dp" android:layout_gravity="center" android:singleLine="true" android:hint="請輸入IP地址" android:textSize="15dip" android:textColorHint="#AAAAAA" android:layout_margin="20dip" android:padding="5dip" android:background="@drawable/shape" android:inputType="text" /> <EditText android:layout_width="280dp" android:layout_height="40dp" android:id="@+id/gjDateEdittext" android:layout_gravity="center" android:singleLine="true" android:hint="請輸入秒數" android:textSize="15dip" android:textColorHint="#AAAAAA" android:layout_margin="20dip" android:padding="5dip" android:background="@drawable/shape" android:inputType="number" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" android:layout_gravity="center" android:gravity="center" > <Button android:layout_width="150dp" android:layout_height="80dp" android:id="@+id/dsgjBtn" android:text="@string/dsgjbtn" android:layout_marginTop="2dp" /> <Button android:layout_width="150dp" android:layout_height="80dp" android:id="@+id/qxgjBtn" android:layout_toRightOf="@+id/dsgjBtn" android:text="@string/qxgjBtn" android:layout_marginTop="2dp" /> </RelativeLayout> </LinearLayout> </FrameLayout></LinearLayout></TabHost>
第二步,初始化tabhost
在頂部定義變數
private TabHost tabhost;
在onCreate方法中
// 從TabActivity上面擷取放置Tab的TabHosttabhost = getTabHost();tabhost.addTab(tabhost.newTabSpec("one").setIndicator("立刻關機").setContent(R.id.lkgj));tabhost.addTab(tabhost// 建立新標籤one.newTabSpec("two")// 設定標籤標題.setIndicator("定時關機")// 設定該標籤的布局內容.setContent(R.id.dsgj));//點擊事件tabhost.setOnTabChangedListener(new OnTabChangeListener(){ <span style="white-space:pre"></span> @Override <span style="white-space:pre"></span> public void onTabChanged(String tabId){ <span style="white-space:pre"></span> <span style="white-space:pre"></span> } <span style="white-space:pre"></span> });
【Android】Tab頁的實現,利用tabhost