zoukankan      html  css  js  c++  java
  • Android中选项卡功能的实现

     

    Android中选项卡功能的实现

    Android中使用TabHostTabWidget来实现选项卡功能。TabHost必须是布局的根节点,它包含两个子节点:

    TabWidget,显示选项卡;

    FrameLayout,显示标签内容。

    实现选项卡功能有两种方法,一种是将多个View放在同一个Activity中,然后使用使用标签来进行切换。另一种是直接使用标签切换不同的Activity。

    后一种方法更为常用一些。本文也只介绍了后一种方法的实现过程。

    1. 创建一个工程,名字可以叫HelloTabWidget。

    2. 创建多个不同的Activity,用来表示各个标签页中的不同内容。

    3. 为标签设计不同的icon。每个标签应该有两个icon,一个表示选中,一个未选中。将图片放在 res/drawable/文件夹下。然后创建一个相应的

    StateListDrawable,用来实现在选中和未选中直接自动的切换。

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
    3.     <!-- When selected, use grey -->  
    4.     <item android:drawable="@drawable/ic_tab_artists_grey"  
    5.           android:state_selected="true" />  
    6.     <!-- When not selected, use white-->  
    7.     <item android:drawable="@drawable/ic_tab_artists_white" />  
    8. </selector>  

    4. 将main.xml替换为以下内容。
    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:id="@android:id/tabhost"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent">  
    6.     <LinearLayout  
    7.         android:orientation="vertical"  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="fill_parent"  
    10.         android:padding="5dp">  
    11.         <TabWidget  
    12.             android:id="@android:id/tabs"  
    13.             android:layout_width="fill_parent"  
    14.             android:layout_height="wrap_content" />  
    15.         <FrameLayout  
    16.             android:id="@android:id/tabcontent"  
    17.             android:layout_width="fill_parent"  
    18.             android:layout_height="fill_parent"  
    19.             android:padding="5dp" />  
    20.     </LinearLayout>  
    21. </TabHost>  

    5. 让你的主Activity继承自TabActivity

    6. 在主Activity的onCreate方法中加入标签

    [java] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState) {  
    2.     super.onCreate(savedInstanceState);  
    3.     setContentView(R.layout.main);  
    4.   
    5.     Resources res = getResources(); // Resource object to get Drawables  
    6.     TabHost tabHost = getTabHost();  // The activity TabHost  
    7.     TabHost.TabSpec spec;  // Resusable TabSpec for each tab  
    8.     Intent intent;  // Reusable Intent for each tab  
    9.   
    10.     // Create an Intent to launch an Activity for the tab (to be reused)  
    11.     intent = new Intent().setClass(this, ArtistsActivity.class);  
    12.   
    13.     // Initialize a TabSpec for each tab and add it to the TabHost  
    14.     spec = tabHost.newTabSpec("artists").setIndicator("Artists",  
    15.                       res.getDrawable(R.drawable.ic_tab_artists))  
    16.                   .setContent(intent);  
    17.     tabHost.addTab(spec);  
    18.   
    19.     // Do the same for the other tabs  
    20.     intent = new Intent().setClass(this, AlbumsActivity.class);  
    21.     spec = tabHost.newTabSpec("albums").setIndicator("Albums",  
    22.                       res.getDrawable(R.drawable.ic_tab_albums))  
    23.                   .setContent(intent);  
    24.     tabHost.addTab(spec);  
    25.   
    26.     intent = new Intent().setClass(this, SongsActivity.class);  
    27.     spec = tabHost.newTabSpec("songs").setIndicator("Songs",  
    28.                       res.getDrawable(R.drawable.ic_tab_songs))  
    29.                   .setContent(intent);  
    30.     tabHost.addTab(spec);  
    31.   
    32.     tabHost.setCurrentTab(2);  
    33. }  

    7. 运行程序即可看到效果。

    总结:

    main.xml中定义的选项卡的样式,它和TabActivity共同配合,创建了选项卡的框架。TabHost.TabSpec 代表了一个选项卡的内容,TabHost使用addTab方法将其加入到整个选项卡中。

    TabHost.TabSpec

    原文: http://blog.csdn.net/cool_android/article/details/7202381

  • 相关阅读:
    bzoj4105: [Thu Summer Camp 2015]平方运算
    bzoj4035: [HAOI2015]数组游戏
    bzoj1022: [SHOI2008]小约翰的游戏John
    bzoj4665: 小w的喜糖
    CodeChef:Little Elephant and Colored Coins
    bzoj4664: Count
    bzoj4498: 魔法的碰撞
    bzoj4230: 倒计时
    bzoj4532: [BeiJing2014 WinterCamp] 珠链
    python 画正态曲线
  • 原文地址:https://www.cnblogs.com/geniusxjq/p/4223688.html
Copyright © 2011-2022 走看看