Android 實現分頁(使用TabWidget/TabHost)

來源:互聯網
上載者:User

標籤:

註:本文為轉載,但該內容本人已親身嘗試,確認該方法可行,代碼有點小的改動,轉載用作儲存與分享。

原作者地址: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.