新聞中心
如何創(chuàng)建tab android
您好,很高興為您解答:
為企業(yè)提供做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)站優(yōu)化、成都營銷網(wǎng)站建設(shè)、競價托管、品牌運營等營銷獲客服務(wù)。創(chuàng)新互聯(lián)建站擁有網(wǎng)絡(luò)營銷運營團隊,以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!
在創(chuàng)建Tab之前,先把Tab的結(jié)構(gòu)搞清楚。它的結(jié)構(gòu)是這樣的:
最外層是一個Tabhost,Tabhost里裝了些選項卡(TabSpec),每個選項卡有自己的指示符(Indicator,就是頂部可點的那個區(qū)塊)和內(nèi)容(Content,下半部分展示內(nèi)容的區(qū)塊)。
現(xiàn)在,要做的事情就很清楚了:
1、創(chuàng)建Tabhost
2、創(chuàng)建TabSpec并給TabSpec賦值
3、把TabSpec添加到Tabhost中
如果我的回答沒能幫助您,請繼續(xù)追問。
安卓手機上如何打出 tab鍵
在百度輸入法里下載一個叫“apple風(fēng)格點選布局”的皮膚,這個皮膚最上面有一排數(shù)字鍵,數(shù)字“8”的下面就是tab符,在數(shù)字8下面往下滑就能輸入tab符。
android中tab選項卡怎么做
第一步
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"選項卡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:一個FrameLayout對象好比一塊在屏幕上提前預(yù)定好的空白區(qū)域,
然后可以填充一些元素到里邊,比方說一張圖片等。
需要注意的是所有元素都被放置在FrameLayout區(qū)域的左上的區(qū)域,
而且無法為這些元素指定一個確切的位置。如果有多個元素,則后邊的會重疊在前一個元素上。
android:gravity用于設(shè)置View組件的對齊方式
(另外,android:layout_gravity用于設(shè)置Container組件的對齊方式)
center_horizontal 不改變控件大小,對其到容器橫向中間位置(也就是在豎直方向的中間)
android:scaleType="fitXY" 把圖片不按比例來擴大或者縮小顯示
--
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(),
* 不同點是LayoutInflater是用來找layout文件夾下的xml布局文件,并且實例化
* 注:findViewById()只是找控件之類(如Button和EditView)
*
* LayoutInflater.from(this)獲得context實例
* 也就是相當(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的后臺運行在很多service,
* 它們在系統(tǒng)啟動時被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即是選項卡的指示符,對于TabSpec可以設(shè)置一個標(biāo)題或者設(shè)置一個標(biāo)題和圖標(biāo)
* setIndicator 是為選項卡指示符指定一個標(biāo)簽和圖標(biāo)
* setContent 為選項卡的內(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
附上出處鏈接:
android 如何實現(xiàn)自定義tab?? 如圖
實現(xiàn)自定義tab過程如下:
1.制作4個9patch的tab樣式,可參考android默認(rèn)的資源
tab_unselected.9.png?tab_selected.9.pngtab_press.9.pngtab_focus.9.png
這4個資源分別代表Tab的4種狀態(tài)。
2.定義Tab的selector樣式(就叫它tab_indicator.xml好了),將其放入drawable文件夾下,代碼如下:
xml?version="1.0"?encoding="utf-8"???
selector?xmlns:android=""??
item?android:state_focused="false"?android:state_selected="false"?android:state_pressed="false"?android:drawable="@drawable/tab_unselected"?/?
item?android:state_focused="false"?android:state_selected="true"?android:state_pressed="false"?android:drawable="@drawable/tab_selected"?/?
item?android:state_focused="true"?android:state_selected="false"?android:state_pressed="false"?android:drawable="@drawable/tab_focus"?/??
item?android:state_focused="true"?android:state_selected="true"?android:state_pressed="false"?android:drawable="@drawable/tab_focus"?/???
item?android:state_pressed="true"?android:drawable="@drawable/tab_press"?/??
selector??
3.編寫indicator的布局文件(不妨也叫tab_indicator.xml),將其放入layout文件夾下,代碼如下:
xml?version="1.0"?encoding="utf-8"???
RelativeLayout?xmlns:android=""??
android:layout_width="0dip"??
android:layout_height="64dip"??
android:layout_weight="1"??
android:layout_marginLeft="-3dip"??
android:layout_marginRight="-3dip"??
android:orientation="vertical"??
android:background="@drawable/tab_indicator"??
ImageView?android:id="@+id/icon"??
android:layout_width="wrap_content"??
android:layout_height="wrap_content"??
android:layout_centerHorizontal="true"??
/??
TextView?android:id="@+id/title"??
android:layout_width="wrap_content"??
android:layout_height="wrap_content"??
android:layout_alignParentBottom="true"??
android:layout_centerHorizontal="true"??
style="?android:attr/tabWidgetStyle"?mce_style="?android:attr/tabWidgetStyle"??
/??
4.接下來就是在TabActivity中使用我們自己編寫的Tab樣式了:
//?首先獲取TabWidget??
mTabHost?=?getTabHost();??
LinearLayout?ll?=?(LinearLayout)mTabHost.getChildAt(0);??
TabWidget?tw?=?(TabWidget)ll.getChildAt(0);??
RelativeLayout?tabIndicator1?=?(RelativeLayout)?LayoutInflater.from(this).inflate(R.layout.tab_indicator,?tw,?false);??
TextView?tvTab1?=?(TextView)tabIndicator1.getChildAt(1);??
tvTab1.setText("tab1");??
mTabHot?=?mTabHost.newTabSpec("TAB_1")??
.setIndicator(tabIndicator1)??
.setContent(contentIntent);
當(dāng)前標(biāo)題:關(guān)于tabandroid的信息
鏈接URL:http://ef60e0e.cn/article/dscjcsg.html