android tabs找不到声明,Android选项卡实现之TabHost/TabSpec/TabWidget

1.继承TabActivity html

2.布局文件中使用tabHost,tabWedgit和framework java

3.在activity中经过源码添加tab选项卡,每一个选项卡中显示指定activity中的内容。能够经过代码控制界面的显示效果。 android

下面仍是经过一个代码示例来讲明下吧: git

(1)建立一个新的工程TestTab吧 app

(2)将生成的MainActivity的继承类改为TabActivity,若是没自动生成mainactivity,那就本身手动建立一个吧: ide

publicclassMainActivityextendsTabActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

}

}

(3)建立main的布局文件 布局

注意:TabHost ,TabWidget ,FrameLayout的ID必须分别为@android :id/tabhost,@android :id/tabs,@android :id/tabcontent

另外还要注意一下android:layout_width宽度和android:layout_height高度的取值,还要LinearLayout的android:orientation=”vertical”(LinearLayout默认是横向的)当你看到布局和我不同时你就要考虑一下这里是否是错了。(= =!由于我错过) flex

这里面的TabHost就是tab区加上对应的属性页;TabWidget就是Tab按钮区,FrameLayout就是属性页区域. ui

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@android :id/tabhost"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

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

android:layout_width="fill_parent"

android:layout_height="0dp"

android:layout_weight="1" />

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="0"

android:orientation="horizontal" />

(4)建立tab元素:TabSpec this

这里面会涉及到对象TabSpec。该对象一般用TabHost来建立,里面包含了一个Tab元素所包含的信息须要用户来指定。下面来看看官方文档的解释吧:

A tab has a tab indicator, content, and a tag that is used to keep track of it. This builder helps choose among these options. For the tab indicator, your choices are: 1) set a label 2) set a label and an icon For the tab content, your choices are: 1) the id of aView2) aTabHost.TabContentFactorythat creates theViewcontent. 3) anIntentthat launches anActivity.

一个tab一般包含了indicator(指示器)、content(tab对应展现的页面view,能够为这个view的id或者是一个intent)、tag。其中TabSpec就是用来辅助设置这些选项。:

<1> Indicator一般是设置tab的名称label和icon。

<2> Content:能够是一个view的id,也能够经过TabContentFactory建立一个view,还能够是一个Intent组件来加载一个activity。

(5)建立两个Activity,暂且叫FirstActivity,SecActivity,并在Manifest文件中注册

publicclassSettingActivityextendsActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView textview = new TextView(this);

textview.setText("设置页面");

setContentView(textview);

} }

(6)在MainActivity中添加Tab -- 使用TabSpec

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mTabHost = getTabHost();

TabSpec spec;

spec = mTabHost.newTabSpec("新建");

spec.setIndicator("新建");

spec.setContent(new Intent(this, NewTaskActivity.class));

mTabHost.addTab(spec);

spec = mTabHost.newTabSpec("设置");

spec.setIndicator("设置");

spec.setContent(new Intent(this, SettingActivity.class));

mTabHost.addTab(spec);

mTabHost.setCurrentTab(0); }

至此,一个简单的tab就作完了,运行下就能够看到结果。你也能够根据本身的喜爱更改下每一个tab的内容的activity样子。

(7)TabWidget说明:

经过上面的类继承关系就能够看出TabWidget是一个ViewGroup,便是一个线性布局的容器,可以容纳多个Tab。同时“The container object for this widget is TabHost”。它也是TabHost内的一个widget。

不过用过API4.0的小伙伴必定都会发现TabActivity已经被android弃用了。

TabActivity has been deprecated, cause it is subclass of ActivityGroup, which also has been deprecated.

ActityGroup has been deprecated, andinstead Fragment has been introduced and suggested.As using Fragments is easier and more flexible than ActivityGroup. It also enable android components to have a homogeneous pattern.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值