網站首頁 編程語言 正文
本文實例為大家分享了android選項卡TabHost功能用法,供大家參考,具體內容如下
首先定義三個xml文件,分別為l1.xml,l2.xml,l3.xml,每個選項卡的標簽頁的內容為一個xml文件。
其中l1.xml為:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical"? ? ? android:id="@+id/l1"> ? ? ? <Button ? ? ? ? android:id="@+id/button1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="Button" /> ? </LinearLayout>
l2.xml為:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical"? ? ? android:id="@+id/l2"> ? ? ? <EditText ? ? ? ? android:id="@+id/editText1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:ems="10" ? ? ? ? android:inputType="textPersonName" > ? ? ? ? ? <requestFocus /> ? ? </EditText> ? ? ? <EditText ? ? ? ? android:id="@+id/editText2" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:ems="10" ? ? ? ? android:inputType="textPassword" /> ? </LinearLayout>
l3.xml為:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? android:id="@+id/l3"> ? ? ? <RatingBar ? ? ? ? android:id="@+id/ratingBar1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" /> ? </LinearLayout>
TabActivityDemo2.java:
package com.example.wenandroid;
?
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost.TabSpec;
?
public class TabActivityDemo2 extends TabActivity {
?
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?android.widget.TabHost tab=getTabHost();
?? ??? ?LayoutInflater lay=LayoutInflater.from(this);
?? ??? ?lay.inflate(R.layout.l1, tab.getTabContentView(),true);
?? ??? ?lay.inflate(R.layout.l2, tab.getTabContentView(),true);
?? ??? ?lay.inflate(R.layout.l3, tab.getTabContentView(),true);
?? ??? ?
?? ??? ?TabSpec t1=tab.newTabSpec("t1");
?? ??? ?TabSpec t2=tab.newTabSpec("t2");
?? ??? ?TabSpec t3=tab.newTabSpec("t3");
?? ??? ?
?? ??? ?t1.setIndicator("選項卡1");
?? ??? ?t2.setIndicator("選項卡2");
?? ??? ?t3.setIndicator("選項卡3");
?? ??? ?
?? ??? ?t1.setContent(R.id.l1);
?? ??? ?t2.setContent(R.id.l2);
?? ??? ?t3.setContent(R.id.l3);
?? ??? ?
?? ??? ?tab.addTab(t1);
?? ??? ?tab.addTab(t2);
?? ??? ?tab.addTab(t3);
?? ?}
?
}
效果如下圖:
原文鏈接:https://blog.csdn.net/zhu_hua_jie/article/details/9411789
相關推薦
- 2022-01-13 使用postcss插件配置rem和手寫rem的方法
- 2022-07-02 在React中使用axios發送請求
- 2022-07-24 C++超詳細實現二叉樹的遍歷_C 語言
- 2022-04-16 C語言自定義類型全解析_C 語言
- 2022-08-04 淺析.net?core?拋異常對性能影響_實用技巧
- 2022-12-02 Android?Flutter實現自由落體彈跳動畫效果_Android
- 2023-08-28 vscode里面報:‘xxx‘ is assigned a value but never used
- 2023-01-05 Go語音開發中常見Error類型處理示例詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支