android用户界面-组件Widget-选项卡Tab

简介:

使用Tab的步骤:
1、在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件。

2、Activity要继承TabActivity。

3、调用TabActivity的getTabHost()方法获得TabHost对象。

4、通过TabHost创建Tab选项。

 

/Chapter04_UI_Tab01/src/com/amaker/test/MainActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.TabActivity;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.Window;  
  9. import android.view.WindowManager;  
  10. import android.widget.TabHost;  
  11. import android.widget.Toast;  
  12. import android.widget.TabHost.OnTabChangeListener;  
  13.  
  14. public class MainActivity extends TabActivity {  
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.        /* requestWindowFeature(Window.FEATURE_NO_TITLE);  
  20.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  21.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);*/ 
  22.           
  23.         TabHost th = getTabHost();  
  24.           
  25.         LayoutInflater.from(this).inflate(R.layout.main, th.getTabContentView(), true);  
  26.           
  27.         th.addTab(th.newTabSpec("all").setIndicator("所有通话记录").setContent(R.id.TextView01));  
  28.         th.addTab(th.newTabSpec("ok").setIndicator("已接来电").setContent(R.id.TextView02));  
  29.         th.addTab(th.newTabSpec("cancel").setIndicator("未接来电").setContent(R.id.TextView03));  
  30.       
  31.           
  32.         th.setOnTabChangedListener(  
  33.                 new OnTabChangeListener() {  
  34.                     @Override 
  35.                     public void onTabChanged(String tabId) {  
  36.                         Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_LONG).show();  
  37.                     }  
  38.                 }  
  39.         );  
  40.           
  41.           
  42.       
  43.     }  

/Chapter04_UI_Tab01/res/layout/main.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4.  
  5. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  6.     android:id="@+id/FrameLayout01"   
  7.     android:layout_width="wrap_content"   
  8.     android:layout_height="wrap_content"> 
  9.       
  10.     <TextView   
  11.     android:id="@+id/TextView01"   
  12.     android:layout_width="wrap_content"   
  13.     android:layout_height="wrap_content"   
  14.     android:text="所有通话记录"></TextView> 
  15.       
  16.     <TextView   
  17.     android:id="@+id/TextView02"   
  18.     android:layout_width="wrap_content"   
  19.     android:layout_height="wrap_content"   
  20.     android:text="已接来电"></TextView> 
  21.       
  22.     <TextView   
  23.     android:id="@+id/TextView03"   
  24.     android:layout_width="wrap_content"   
  25.     android:layout_height="wrap_content"   
  26.     android:text="未接来电"></TextView> 
  27.       
  28. </FrameLayout> 

通过实现一个接口TabHost.TabContentFactory的createTabContent方法来制定Tab的内容

/Chapter04_UI_Tab02/src/com/amaker/test/MainActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.  
  8. import android.app.TabActivity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.ListView;  
  13. import android.widget.TabHost;  
  14.  
  15. public class MainActivity extends TabActivity implements 
  16.         TabHost.TabContentFactory {  
  17.     /** Called when the activity is first created. */ 
  18.     @Override 
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         TabHost th = getTabHost();  
  22.         th.addTab(th.newTabSpec("all").setIndicator("所有通话记录").setContent(this));  
  23.         th.addTab(th.newTabSpec("ok").setIndicator("已接来电").setContent(this));  
  24.         th  
  25.                 .addTab(th.newTabSpec("cancel").setIndicator("未接来电")  
  26.                         .setContent(this));  
  27.     }  
  28.  
  29.     public View createTabContent(String tag) {  
  30.         ListView lv = new ListView(this);  
  31.         List<String> list = new ArrayList<String>();  
  32.         list.add(tag);  
  33.         if(tag.equals("all")){  
  34.             list.add("tom");  
  35.             list.add("kite");  
  36.             list.add("rose");  
  37.         }else if(tag.equals("ok")){  
  38.             list.add("tom");  
  39.             list.add("kite");  
  40.         }else{  
  41.             list.add("rose");  
  42.         }  
  43.           
  44.         ArrayAdapter adapter = new ArrayAdapter(this,  
  45.                 android.R.layout.simple_list_item_checked, list);  
  46.         lv.setAdapter(adapter);  
  47.         return lv;  
  48.     }  

/Chapter04_UI_Tab02/res/layout/main.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9. <TextView    
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="@string/hello" 
  13.     /> 
  14. </LinearLayout> 

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080696


相关文章
|
4天前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
17 2
|
14天前
|
JavaScript Java Android开发
kotlin安卓在Jetpack Compose 框架下跨组件通讯EventBus
**EventBus** 是一个Android事件总线库,简化组件间通信。要使用它,首先在Gradle中添加依赖`implementation &#39;org.greenrobot:eventbus:3.3.1&#39;`。然后,可选地定义事件类如`MessageEvent`。在活动或Fragment的`onCreate`中注册订阅者,在`onDestroy`中反注册。通过`@Subscribe`注解方法处理事件,如`onMessageEvent`。发送事件使用`EventBus.getDefault().post()`。
|
23天前
|
XML API Android开发
android上FragmentTabHost实现自定义Tab Indicator
android上FragmentTabHost实现自定义Tab Indicator
19 1
|
23天前
|
XML Android开发 数据格式
【Android UI】中间对齐UI组件
【Android UI】中间对齐UI组件
13 1
|
2天前
|
存储 缓存 Android开发
安卓应用开发:打造高效用户界面的五大技巧
【5月更文挑战第59天】 在移动应用的世界里,一个流畅且直观的用户界面(UI)是成功的关键。本文深入探讨了五个关键的UI设计技巧,这些技巧可以帮助安卓开发者优化其应用的性能和用户体验。通过减少不必要的绘制操作、使用高效的布局设计、合理管理内存资源、采用合适的数据存储方案以及充分利用硬件加速功能,开发者能够显著提升应用的响应速度和稳定性。这些技巧不仅适用于新手开发者,对于有经验的开发者来说,也是值得复习和实践的重要知识点。
|
2天前
|
搜索推荐 Android开发 开发者
Android 自定义组件
Android 自定义组件
6 0
|
3天前
|
存储 程序员 定位技术
程序员必知:安卓的四大组件
程序员必知:安卓的四大组件
|
20天前
|
机器学习/深度学习 搜索推荐 Android开发
在安卓应用开发中,构建高效的用户界面是至关重要的一环
【6月更文挑战第10天】本文是关于构建高效安卓用户界面的指南,分为设计原则和技巧两部分。设计原则包括一致性、简洁性和可访问性,强调遵循安卓系统规范、保持界面简洁及考虑不同用户需求。技巧方面,建议合理布局、优化图标和图片、使用动画效果、提供个性化设置以及优化性能。随着技术发展,开发者需关注AI和机器学习,以创新应用体验,适应用户需求变化。
26 0