源码教程 2025年06月7日
0 收藏 0 点赞 311 浏览 3401 个字
摘要 :

在有限的手机屏幕空间内,当要浏览的内容较多,无法在一个屏幕空间内全部显示时,可以使用滚动视图来延长屏幕的空间。当浏览的内容具有很强的类别性质时,更合适的方法是……

在有限的手机屏幕空间内,当要浏览的内容较多,无法在一个屏幕空间内全部显示时,可以使用滚动视图来延长屏幕的空间。

当浏览的内容具有很强的类别性质时,更合适的方法是将不同类别的内容集中到各自的面板中,这时就需要使用面板标签(Tab)组件了。

Tab 组件利用面板标签把不同的面板内容切换到屏幕上,以显示不同类别的内容。

下面通过一个实例来了解一下 Tab 组件的使用方法。在工程 WidgetDemo 的布局文件 main.xml 中添加一个名为 TabDemo 的 Button,用以启动 TabActivity。

在 main.xml 中添加代码如下:

  1. <Button

  2. android:id="@+id/button13"

  3. android:layout_width="wrap_content"

  4. android:layout_height="wrap_content"

  5. android:text="TabDemo"/>

单击 Button 并启动 GridViewActivity 的代码如下:

  1. Button tabbtn = (Button)this.findViewById(R.id.button13);

  2. tabbtn.setOnClickListener(new View.OnClickListener(){

  3. @Override

  4. public void onClick(View v){

  5. Intent intent;

  6. intent = new Intent(MainActivity.this,TabActivity .class);

  7. startActivity(intent);

  8. }

  9. });

同时在 AndroidManifest.xml 文件中声明该 Activity:

<activity android:name=".TabActivity"/>

TabActivity 的运行效果如图 1 所示。

Android面板标签控件Tab使用

图 1  TabActivity 的运行结果

要使用 Tab 必然涉及它的容器 TabHost,TabHost 包括 TabWigget 和 FrameLayout 两部分。TabWidget 就是每个 Tab 的标签,FrameLayout 是 Tab 的内容。

TabActivity 使用的布局文件是 tab.xml。在 tab.xml 中定义了每个 Tab 中要显示的内容,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:id="@+id/tabhost"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent">

  6. <LinearLayout

  7. android:layout_width="fill_parent"

  8. android:layout_height="fill_parent"

  9. android:orientation="vertical">

  10. <TabWidget

  11. android:id="@android:id/tabs"

  12. android:layout_width="fill_parent"

  13. android:layout_height="wrap_content" />

  14. <FrameLayout

  15. android:id="@android:id/tabcontent"

  16. android:layout_width="fill_parent"

  17. android:layout_height="fill_parent">

  18. <TextView

  19. android:id="@+id/tab1"

  20. android:layout_width="wrap_content"

  21. android:layout_height="wrap_content"

  22. android:text="Tab1页面"

  23. android:textSize="40dp" />

  24. <TextView

  25. android:id="@+id/tab2"

  26. android:layout_width="wrap_content"

  27. android:layout_height="wrap_content"

  28. android:text="Tab2页面"

  29. android:textSize="40dp" />

  30. <TextView

  31. android:id="@+id/tab3"

  32. android:layout_width="wrap_content"

  33. android:layout_height="wrap_content"

  34. android:text="Tab3页面"

  35. android:textSize="40dp" />

  36. </FrameLayout>

  37. </LinearLayout>

  38. </TabHost>

在 FrameLayout 中我们放置了三个 TextView 组件,分别对应三个 Tab 所显示的内容,当切换不同的 Tab 时会自动显示不同的 TextView 内容。

在主程序 TabActivity 的 OnCreate() 方法中,首先获得 TabHost 的对象,并调用 setup() 方法进行初始化,然后通过 TabHost.TabSpec 增加 Tab 页,通过 setContent() 增加当前 Tab 页显示的内容,通过 setIndicator 增加页的标签,最后设定当前要显示的 Tab 页。

TabActivity 的代码如下:

  1. package introduction.android.widgetdemo;

  2. import android.app.Activity;

  3. import android.os.Bundle;

  4. import android.widget.TabHost;

  5. public class TabActivity extends Activity {

  6. public void onCreate(Bundle savedInstanceState) {

  7. super.onCreate(savedInstanceState);

  8. setContentView(R.layout.tab);

  9. // 步骤1:获得TabHost的对象,并进行初始化setup()

  10. TabHost tabs = (TabHost) findViewById(R.id.tabhost);

  11. tabs.setup();

  12. //步骤2:获得TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签

  13. /*增加第一个Tab */

  14. TabHost.TabSpec spec = tabs.newTabSpec("Tag1");

  15. //单击Tab要显示的内容

  16. spec.setContent(R.id.tab1);

  17. /* 显示Tabl内容*/

  18. spec.setIndicator("Tab1");

  19. tabs.addTab(spec);

  20. /* 增加第二个Tab*/

  21. spec = tabs.newTabSpec("Tag2");

  22. spec.setContent(R.id.tab2);//单击Tab要显示的内容

  23. /* 显示Tab2内容 */

  24. spec.setIndicator("Tab2");

  25. tabs.addTab(spec);

  26. /*增加第三个Tab */

  27. spec = tabs.newTabSpec("Tag3");

  28. spec.setContent(R.id.tab3);//单击Tab要显示的内容

  29. /* 显示Tab3内容*/

  30. spec.setIndicator("Tab3");

  31. tabs.addTab(spec);

  32. /* 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算*/

  33. tabs.setCurrentTab(0);

  34. }

  35. }

除了使用上述方法设置 Tab 页面的显示内容外,还可以使用 setContent(Intent)方法启动某个 Activity,并将该 Activity 的视图作为 Tab 页面的内容。

例如:

  1. Intent intent = new Intent().setClass(this,AlbumsActivity .class);

  2. spec = tabHost.newTabSpec("albums")

  3. .setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);

  4. tabHost.addTab(spec);

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/1004.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

700
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

714
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

305
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,038
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

834
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

721
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号