Android UI控制元件系列:TabWidget(切換卡)
Tab選項卡類似與電話本的介面,通過多個標籤切換不同的內容,要實現這個效果,首先要知道TabHost,它是一個用來存放多個Tab標籤的容器,每一個Tab都可以對應自己的佈局,比如,電話本中的Tab佈局就是一個線性佈局
要使用TabHost,首先要通過getTabHost方法獲取TabHost的物件,然後通過addTab方法來向TabHost中新增Tab,當然每個Tab在切換時都會產生一個事件,要捕捉這個事件,需要設定TabActivity的事件監聽setOnTabChangedListener
下面是個小例子:
TabTest.java
package org.hualang.tab; import android.app.Activity; import android.app.TabActivity; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener; public class TabTest extends TabActivity { /** Called when the activity is first created. */ TabHost tabhost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost物件 tabhost = getTabHost(); //為TabHost新增標籤 //新建一個newTabSpec(newTabSpec) //設定其標籤和圖示(setIndicator) //設定內容(setContent) tabhost.addTab(tabhost.newTabSpec("tab1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.text1)); tabhost.addTab(tabhost.newTabSpec("tab2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.text2)); tabhost.addTab(tabhost.newTabSpec("tab3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.text3)); //設定TabHost的背景顏色 //tabhost.setBackgroundColor(Color.argb(150,22,70,150)); //設定TabHost的背景圖片資源 tabhost.setBackgroundResource(R.drawable.bg0); //設定當前顯示哪個標籤 tabhost.setCurrentTab(0); //標籤切換事件處理,setOnTabChangedListener tabhost.setOnTabChangedListener(new OnTabChangeListener() { public void onTabChanged(String tabId) { Toast toast=Toast.makeText(getApplicationContext(), "現在是"+tabId+"標籤", Toast.LENGTH_SHORT); toast.show(); } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" 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"> <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項卡1" /> <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項卡2" /> <TextView android:id="@+id/text3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項卡3" /> </FrameLayout> </LinearLayout> </TabHost>
執行效果如下:
相關文章
- Android UI控制元件系列:Toast(提示)AndroidUI控制元件AST
- Android UI控制元件系列:Button(按鈕)AndroidUI控制元件
- Android UI控制元件系列:TextView(文字框)AndroidUI控制元件TextView
- Android UI控制元件系列:TableLayout(表格佈局)AndroidUI控制元件
- Android UI控制元件系列:Spinner(下拉選單)AndroidUI控制元件
- Android UI控制元件系列:ProgressBar(進度條)AndroidUI控制元件
- Android UI控制元件系列:Gallery(畫廊檢視)AndroidUI控制元件
- Android UI控制元件系列:WebView(網路檢視)AndroidUI控制元件WebView
- Android UI控制元件系列:AutoCompleteTextView(自動提示)AndroidUI控制元件TextView
- Android UI控制元件系列:Tab Layout(選項卡布局)AndroidUI控制元件
- Android UI控制元件系列:RelativeLayout(相對佈局)AndroidUI控制元件
- Android UI控制元件系列:GridView(網格佈局)AndroidUI控制元件View
- Android UI控制元件系列:Dialog(對話方塊)AndroidUI控制元件
- Android UI 開發之實現底部切換標籤AndroidUI
- Android UI設計(7):ListView 控制元件與 Adapter 介面卡AndroidUIView控制元件APT
- 如何實現選項卡切換
- Android UI控制元件系列:LinearLayout(線性佈局)AndroidUI控制元件
- Android UI控制元件系列:ImageButton(帶圖示的按鈕)AndroidUI控制元件
- Android UI控制元件系列:RadioButton(單選按鈕)AndroidUI控制元件
- 華為Mate 20 Pro雙卡怎麼切換?華為Mate20 Pro雙卡資料切換方法教程
- 利用ViewPager和Fragment實現頁卡切換ViewpagerFragment
- tab選項卡切換例項程式碼
- Android橫豎屏切換Android
- 如何優雅阻止view UI 的 Switch 切換?ViewUI
- iOS開發系列--檢視切換iOS
- CSS3 tab選項卡動態切換CSSS3
- Android UI控制元件系列:DatePicker,TimePicker(日期和時間選擇)AndroidUI控制元件
- Android 介面(1):UI 開發控制元件AndroidUI控制元件
- Android UI 設計(4):EditText 控制元件AndroidUI控制元件
- Flutter UI使用Provide實現主題切換FlutterUIIDE
- Android切換Activity的動畫效果Android動畫
- Android 如何切換到 Transform API?AndroidORMAPI
- JavaScript左右滑動切換的選項卡詳解JavaScript
- Selenium系列教程-09 如何切換iframe
- 【Android 進階】仿抖音系列之翻頁上下滑切換視訊(一)Android
- Android高階UI系列(2)-DecorViewAndroidUIView
- Android UI系列-----EditText和AutoCompleteTextViewAndroidUITextView
- 快速切換至Kotlin for Android模式KotlinAndroid模式