Android Tab 控件详解及实例

所属分类: 软件编程 / Android 阅读数: 20
收藏 0 赞 0 分享

Android Tab 控件详解及实例

在桌面应用中Tab控件使用得非常普遍,那么我们经常在Android中也见到以Tab进行布局的客户端。那么Android中的Tab是如何使用的呢?

1.Activity

package com.wicresoft.activity; 
 
import com.wicresoft.myandroid.R; 
 
import android.app.TabActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 
 
@SuppressWarnings("deprecation") 
public class mainActivity extends TabActivity { 
  private TabHost tabhost; 
  @Override 
  protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 
    //从TabActivity上面获取放置Tab的TabHost 
    tabhost = getTabHost(); 
    tabhost.addTab(tabhost 
         //创建新标签one 
        .newTabSpec("one") 
        //设置标签标题 
        .setIndicator("红色") 
        //设置该标签的布局内容 
        .setContent(R.id.widget_layout_red)); 
    tabhost.addTab(tabhost 
        .newTabSpec("two") 
        .setIndicator("黄色") 
        .setContent(R.id.widget_layout_yellow)); 
    tabhost.addTab(tabhost 
        .newTabSpec("three") 
        .setIndicator("蓝色") 
        .setContent(R.id.widget_layout_blue)); 
    tabhost.setOnTabChangedListener(listener); 
  } 
   
  private OnTabChangeListener listener = new OnTabChangeListener(){ 
    @Override 
    public void onTabChanged(String arg0) { 
      // TODO Auto-generated method stub 
      Log.i("TabChange", arg0); 
    } 
  }; 
} 

2.布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@android:id/tabhost">
  
<LinearLayout 
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  
  <TabWidget 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs">
  </TabWidget>
  
  <FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@android:id/tabcontent">
    <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/widget_layout_red"
      android:background="#ff0000"
      android:orientation="vertical">
    </LinearLayout>
    
     <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/widget_layout_yellow"
      android:background="#FCD209"
      android:orientation="vertical">
     </LinearLayout>
     <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/widget_layout_blue"
      android:background="#0000E3"
      android:orientation="vertical">
     </LinearLayout>
  </FrameLayout>
 </LinearLayout>
</TabHost>

3.Manifest文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.wicresoft.myandroid"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.wicresoft.activity.mainActivity"
      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>

</manifest>

4.效果



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多