Android中使用TabHost和TabWidget來實(shí)現(xiàn)選項(xiàng)卡功能。TabHost必須是布局的根節(jié)點(diǎn),它包含兩個(gè)子節(jié)點(diǎn):
創(chuàng)新互聯(lián)專注于青羊網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供青羊營銷型網(wǎng)站建設(shè),青羊網(wǎng)站制作、青羊網(wǎng)頁設(shè)計(jì)、青羊網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造青羊網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供青羊網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
TabWidget,顯示選項(xiàng)卡;
FrameLayout,顯示標(biāo)簽內(nèi)容。
實(shí)現(xiàn)選項(xiàng)卡功能有兩種方法,一種是將多個(gè)View放在同一個(gè)Activity中,然后使用使用標(biāo)簽來進(jìn)行切換。另一種是直接使用標(biāo)簽切換不同的Activity。
后一種方法更為常用一些。
1. 創(chuàng)建一個(gè)工程,名字可以叫HelloTabWidget。
2. 創(chuàng)建多個(gè)不同的Activity,用來表示各個(gè)標(biāo)簽頁中的不同內(nèi)容。
3. 為標(biāo)簽設(shè)計(jì)不同的icon。每個(gè)標(biāo)簽應(yīng)該有兩個(gè)icon,一個(gè)表示選中,一個(gè)未選中。將圖片放在 res/drawable/文件夾下。然后創(chuàng)建一個(gè)相應(yīng)的
StateListDrawable,用來實(shí)現(xiàn)在選中和未選中直接自動(dòng)的切換。
給TAB卡做個(gè)布局,在布局里面把TextView設(shè)置成居中 給個(gè)事例給你看下 TabHost.TabSpec localTabSpec1 = this.tabHost.newTabSpec("1"); //R.Layout.tab_indicator 就是TAB選項(xiàng)卡的布局View localView = getLayoutInflater().inflate(R.layout.tab_indicator, null);ImageView localImageView = (ImageView) localView.findViewById(R.id.icon);localImageView.setImageResource(R.drawable.menu_ranking);TabHost.TabSpec localTabSpec2 = localTabSpec1.setIndicator(localView);Intent localIntent = new Intent(this, RankActivity.class);localTabSpec2.setContent(localIntent);this.tabHost.addTab(localTabSpec1);
在布局中用TabHost,必須有一個(gè)TabWidget的子控件,并且控件id一定要是android預(yù)先定義的“android:id/tabs”,你只寫一個(gè)TabHost當(dāng)然會報(bào)這個(gè)錯(cuò)
第一步
res/values/strings.xml
[xhtml] view plain copy
?xml version="1.0" encoding="utf-8"?
resources
string name="hello"Hello World, MyTabActivity!/string
string name="app_name"選項(xiàng)卡Demo/string
string name="andy"Andy Rubin--a href="" class='replace_word' title="undefined" target='_blank' style='color:#df3434; font-weight:bold;'Android/a的創(chuàng)造者/string
string name="bill"Bill Joy--Java的創(chuàng)造者/string
string name="torvalds"Linus Torvalds --Linux之父/string
/resources
第二步
res/layout/tab_layout.xml
[xhtml] view plain copy
?xml version="1.0" encoding="utf-8"?
!--
FrameLayout:一個(gè)FrameLayout對象好比一塊在屏幕上提前預(yù)定好的空白區(qū)域,
然后可以填充一些元素到里邊,比方說一張圖片等。
需要注意的是所有元素都被放置在FrameLayout區(qū)域的左上的區(qū)域,
而且無法為這些元素指定一個(gè)確切的位置。如果有多個(gè)元素,則后邊的會重疊在前一個(gè)元素上。
android:gravity用于設(shè)置View組件的對齊方式
(另外,android:layout_gravity用于設(shè)置Container組件的對齊方式)
center_horizontal 不改變控件大小,對其到容器橫向中間位置(也就是在豎直方向的中間)
android:scaleType="fitXY" 把圖片不按比例來擴(kuò)大或者縮小顯示
--
FrameLayout xmlns:android=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
LinearLayout android:id="@+id/linearLayout1"
xmlns:android=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
ImageView
android:id="@+id/imageView01"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/andy"/
TextView
android:id="@+id/testView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/andy"
/
/LinearLayout
LinearLayout android:id="@+id/linearLayout2"
xmlns:android=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
ImageView
android:id="@+id/imageView02"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bill"/
TextView
android:id="@+id/testView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/bill"
/
/LinearLayout
LinearLayout android:id="@+id/linearLayout3"
xmlns:android=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
ImageView
android:id="@+id/imageView03"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/torvalds"/
TextView
android:id="@+id/testView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/torvalds"
/
/LinearLayout
/FrameLayout
第三步
src/com/myandroid/tab/MyTabActivity.java
[java] view plain copy
package com.myandroid.tab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MyTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = this.getTabHost();
/*
* LayoutInflater的作用類似于 findViewById(),
* 不同點(diǎn)是LayoutInflater是用來找layout文件夾下的xml布局文件,并且實(shí)例化
* 注:findViewById()只是找控件之類(如Button和EditView)
*
* LayoutInflater.from(this)獲得context實(shí)例
* 也就是相當(dāng)于this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
* LAYOUT_INFLATER_SERVICE 取得xml里定義的view
*-----------------------------------------------------------------------
* getSystemService:
* 根據(jù)傳入的NAME來取得對應(yīng)的Object,然后轉(zhuǎn)換成相應(yīng)的服務(wù)對象
* android的后臺運(yùn)行在很多service,
* 它們在系統(tǒng)啟動(dòng)時(shí)被SystemServer開啟,支持系統(tǒng)的正常工作,
* 比如MountService監(jiān)聽是否有SD卡安裝及移除,ClipboardService提供剪切板功能,
* 應(yīng)用程序可以通過系統(tǒng)提供的Manager接口來訪問這些Service提供的數(shù)據(jù)
*-----------------------------------------------------------------------
*
* inflate是把xml表述的layout轉(zhuǎn)化為View
* tabHost.getTabContentView() 獲得Tab標(biāo)簽頁的FrameLayout
* true表示將inflate綁定到根布局元素上
*/
LayoutInflater.from(this)
.inflate(R.layout.tab_layout,
tabHost.getTabContentView(), true);
/*
* tabHost.newTabSpec("Tab1") 創(chuàng)建TabHost.TabSpec,
* TabSpec即是選項(xiàng)卡的指示符,對于TabSpec可以設(shè)置一個(gè)標(biāo)題或者設(shè)置一個(gè)標(biāo)題和圖標(biāo)
* setIndicator 是為選項(xiàng)卡指示符指定一個(gè)標(biāo)簽和圖標(biāo)
* setContent 為選項(xiàng)卡的內(nèi)容指定視圖的ID
*/
tabHost.addTab(
tabHost.newTabSpec("Tab1")
.setIndicator("Tab1", getResources().getDrawable(R.drawable.png1)
).setContent(R.id.linearLayout1)
);
tabHost.addTab(
tabHost.newTabSpec("Tab2")
.setIndicator("Tab2", getResources().getDrawable(R.drawable.png2)
).setContent(R.id.linearLayout2)
);
tabHost.addTab(
tabHost.newTabSpec("Tab3")
.setIndicator("Tab3", getResources().getDrawable(R.drawable.png3)
).setContent(R.id.linearLayout3)
);
}
}
第四步
AndroidManifest.xml
[xhtml] view plain copy
?xml version="1.0" encoding="utf-8"?
manifest xmlns:android=""
package="com.myandroid.tab"
android:versionCode="1"
android:versionName="1.0"
application android:icon="@drawable/icon" android:label="@string/app_name"
activity android:name=".MyTabActivity"
android:label="@string/app_name"
intent-filter
action android:name="android.intent.action.MAIN" /
category android:name="android.intent.category.LAUNCHER" /
/intent-filter
/activity
/application
uses-sdk android:minSdkVersion="8" /
/manifest
附上出處鏈接:
新聞標(biāo)題:android選項(xiàng)卡,android選項(xiàng)卡圖片
標(biāo)題路徑:http://m.rwnh.cn/article44/dscocee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、ChatGPT、靜態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站、建站公司、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)