Android(安卓)动态创建Tab
16lz
2021-01-25
我们在开发Android程序的时候,会经常使用Tab界面,但有些时候我们需要动态创建Tab页,这里就简单介绍创建Tab的方法。
第一步 界面格局设定
<?xml version="1.0" encoding="utf-8"?><TabHostandroid:layout_width="fill_parent"android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/tabhost"><TabWidgetandroid:id="@android:id/tabs"android:layout_width="wrap_content"android:layout_height="wrap_content"></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="65px"><LinearLayoutandroid:id="@+id/content1"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:text="tab1"android:id="@+id/phone"android:layout_width="fill_parent"android:layout_height="wrap_content"></TextView></LinearLayout><LinearLayoutandroid:id="@+id/content2"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:text="tab2"android:id="@+id/mail"android:layout_width="fill_parent"android:layout_height="wrap_content"></TextView></LinearLayout></FrameLayout></TabHost>第二步 编写TabActivity的代码
package eoe.stu.ui.tabactivity;import android.app.TabActivity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class AndroidEggTabActivity extends TabActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);TabHost tabs = getTabHost();TabSpec tab1 = tabs.newTabSpec("tab1");tab1.setIndicator("Android");tab1.setContent(R.id.content1);tabs.addTab(tab1);TabSpec tab2 = tabs.newTabSpec("tab2");tab2.setIndicator("Egg");tab2.setContent(R.id.content2);tabs.addTab(tab2);tabs.setCurrentTab(0);}}
转载:http://www.adobex.com/android/source/details/00000427.htm
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 【Android(安卓)开发教程】GridView
- Android(安卓)-- 官方下拉刷新SwipeRefreshLayout
- android之开机跳过锁屏界面自启动应用
- Android:实现装备购买
- Android与JS通过JSBridge(BridgeWebView )相互调用详解
- Eclipse Android(安卓)代码自动提示功能
- Android(安卓)仿RxDialog自定义DialogFragment
- android 多媒体相关代码概况—(JAVA SCOPE)