本篇文章为大家展示了Android中怎么实现选项卡功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
首先创建Android工程命名自己的Activity为HelloTabWidget
在main.xml或者自己定义的*.xml文件中创建一个TabHost,需要两个元素TabWidget和FrameLayout 通常会把这两个元素放到LinearLayout中。FrameLayout作为改变内容content用的。
注意:TabWidget和FrameLayout 有不同的ID命名空间android:id="@android:id/idnames",这个是必须的因此TabHost才能自动找到它,Activity需要继承TabActivity。
Android选项卡Xml代码
< ?xml version="1.0" encoding="utf-8"?> < TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> < LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> < TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> < FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> < TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a tab" /> < TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is another tab" /> < TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a third tab" /> < /FrameLayout> < /LinearLayout> < /TabHost> < ?xml version="1.0" encoding="utf-8"?> < TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> < LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> < TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> < FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> < TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a tab" /> < TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is another tab" /> < TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a third tab" /> < /FrameLayout> < /LinearLayout> < /TabHost>
Activity需要继承TabActivity
Android选项卡Java代码
public class HelloTabWidget extends TabActivity public class HelloTabWidget extends TabActivity
Android选项卡Java代码
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = getTabHost(); mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.textview1)); mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2)); mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3)); mTabHost.setCurrentTab(0); }
上述内容就是Android中怎么实现选项卡功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。