【Android】Tab頁的實現,利用tabhost

來源:互聯網
上載者:User

標籤:

第一步,布局檔案


<?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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.