Android入門之TabHost與TabWidget實(shí)例解析
本文實(shí)例介紹的是Android的Tab控件,Tab控件可以達(dá)到分頁(yè)的效果,讓一個(gè)屏幕的內(nèi)容盡量豐富,當(dāng)然也會(huì)增加開(kāi)發(fā)的復(fù)雜程度,在有必要的時(shí)候再使用。Android的Tab控件使用起來(lái)有點(diǎn)奇怪,必須包含和按照以下的順序:

TabHost控件->TabWidget(必須命名為tabs)->FrameLayout(必須命名為tabcontent)。
先來(lái)貼出本例運(yùn)行的截圖:

main.xml的源碼如下:
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
<EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
<EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
</FrameLayout>
</TabHost>
java程序源碼如下:
package com.testTab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class testTab extends TabActivity {//基于TabActivity構(gòu)建
Button btnTab1,btnTab2;
EditText edtTab1,edtTab2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = getTabHost();
//設(shè)置Tab1
TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setIndicator("tab1"); // 設(shè)置tab1的名稱
tab1.setContent(R.id.Tab1); // 關(guān)聯(lián)控件
tabs.addTab(tab1); // 添加tab1
btnTab1=(Button)this.findViewById(R.id.btnTab1);
edtTab1=(EditText)this.findViewById(R.id.edtTab1);
btnTab1.setOnClickListener(new ClickEvent());
//設(shè)置Tab2
TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator("tab2");
tab2.setContent(R.id.Tab2);
tabs.addTab(tab2);
btnTab2=(Button)this.findViewById(R.id.btnTab2);
edtTab2=(EditText)this.findViewById(R.id.edtTab2);
btnTab2.setOnClickListener(new ClickEvent());
tabs.setCurrentTab(0);
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if(v==btnTab1)
{
edtTab1.setText("tab1");
}
else if(v==btnTab2)
{
edtTab2.setText("tab2");
}
}
}
}
- Android中實(shí)現(xiàn)可滑動(dòng)的Tab的3種方式
- Android TabWidget切換卡的實(shí)現(xiàn)應(yīng)用
- android TabHost(選項(xiàng)卡)的使用方法
- Android開(kāi)發(fā)筆記 TableLayout常用的屬性介紹
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法實(shí)例分析
- android中TabHost的圖標(biāo)(48×48)和文字疊加解決方法
- Android Tab 控件詳解及實(shí)例
相關(guān)文章
Android 7.0 Nougat不得不知的11項(xiàng)新功能
不得不知的11項(xiàng)Android 7.0 Nougat新功能,感興趣的小伙伴們可以參考一下2016-09-09
android AlertDialog的簡(jiǎn)單使用實(shí)例
本篇文章主要介紹了android AlertDialog的簡(jiǎn)單使用實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android5.0中Material Design的新特性
這篇文章主要介紹了Android5.0中Material Design的新特性的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android實(shí)現(xiàn)自動(dòng)填寫獲取驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)填寫獲取驗(yàn)證碼功能,感興趣的小伙伴們可以參考一下2016-03-03
Android多國(guó)語(yǔ)言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國(guó)語(yǔ)言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01
安卓(android)怎么實(shí)現(xiàn)下拉刷新
這里我們將采取的方案是使用組合View的方式,先自定義一個(gè)布局繼承自LinearLayout,然后在這個(gè)布局中加入下拉頭和ListView這兩個(gè)子元素,并讓這兩個(gè)子元素縱向排列。對(duì)安卓(android)怎么實(shí)現(xiàn)下拉刷新的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-04-04

