当前位置: 移动技术网 > IT编程>移动开发>Android > Android Tab 控件详解及实例

Android Tab 控件详解及实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

张家港市邮编,实践单位意见,拆弹高手

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.效果



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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网