不继承tabactivity 过时,为什么点tabhost没用

8970人阅读
先查看下最终效果图:
再看下代码结构:
其中black.gif顾名思义就是一个黑背景图片,grey.gif就是一张灰色的背景图片
然后直接上代码:
ArtistActivity.java
import android.app.A
import android.os.B
import android.widget.TextV
public class ArtistActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
// 该文档将会作为标签的内容进行显示
textView.setText("艺术内容");
setContentView(textView);
MusicActivity.java
import android.app.A
import android.os.B
import android.widget.TextV
public class MusicActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
// 该文档将会作为标签的内容进行显示
textView.setText("音乐内容");
setContentView(textView);
SportActivity.java
import android.app.A
import android.os.B
import android.widget.TextV
public class SportActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
// 该文档将会作为标签的内容进行显示
textView.setText("运动内容");
setContentView(textView);
ArtistActivity.java& MusicActivity.java& SportActivity.java三个activity是用做标签内容的activity。即当用户点击相应的标签时,下边会显示相应的activity内容。
ic_tab.xml代码
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
&item android:drawable="@drawable/grey"
android:state_selected="true"
&item android:drawable="@drawable/black"
&/selector&
这里一定要注意ic_tab.xml文件的位置,是放在res/drawable文件夹下的。有些朋友说怎么没有这个文件夹啊,实际上大家看到了我将它放在了drawable-hdpi中了,实际上drawable-hdpi、drawable-ldpi、drawable-mdpi三个文件夹都属于drawable文件夹的哦。该文件它规定了,当标签获得焦点和失去焦点时,标签上显示什么图片。
例如本例中,就是当state_selected="true"(当标签被选中时),显示@drawable/grey指定的资源图片。当未被选中时,显示@drawable/black指定的资源图片。
tagView.java代码:
import android.app.TabA
import android.content.I
import android.content.res.R
import android.os.B
import android.widget.TabH
* @author chenzheng_Java
* @description 注意,该类一定要继承TabActivity
public class TagView extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
// android代码中访问application资源的一个类
Resources resources = getResources();
// 获取当前activity的标签,该方法的实现中已经执行了setContentView(com.android.internal.R.layout.tab_content);
TabHost tabHost = getTabHost();
// 每一个标签项
TabHost.TabS
// 声明一个意图,该意图告诉我们,下一个跳转到的activity是ArtistActivity。
Intent intent = new Intent(this, ArtistActivity.class);
* tabHost.newTabSpec("artist")创建一个标签项,其中artist为它的标签标识符,相当于jsp页面标签的name属性
* setIndicator("艺术标签",resources.getDrawable(R.drawable.ic_tab))设置标签显示文本以及标签上的图标(该图标并不是一个图片,而是一个xml文件哦)
* setContent(intent)为当前标签指定一个意图
* tabHost.addTab(spec); 将标签项添加到标签中
spec = tabHost.newTabSpec("artist").setIndicator("艺术标签",
resources.getDrawable(R.drawable.ic_tab)).setContent(intent);
tabHost.addTab(spec);
Intent intent2 = new Intent(this, MusicActivity.class);
spec = tabHost.newTabSpec("music").setIndicator("音乐标签",
resources.getDrawable(R.drawable.ic_tab)).setContent(intent2);
tabHost.addTab(spec);
Intent intent3 = new Intent(this, SportActivity.class);
spec = tabHost.newTabSpec("sport").setIndicator("体育标签",
resources.getDrawable(R.drawable.ic_tab)).setContent(intent3);
tabHost.addTab(spec);
// tabHost.setCurrentTabByTag("music");设置第一次打开时默认显示的标签,该参数与tabHost.newTabSpec("music")的参数相同
tabHost.setCurrentTab(1);//设置第一次打开时默认显示的标签,参数代表其添加到标签中的顺序,位置是从0开始的哦。
AndroidManifest.xml
&?xml version="1.0" encoding="utf-8"?&
&manifest xmlns:android="/apk/res/android"
package="cn.com.tagview"
android:versionCode="1"
android:versionName="1.0"&
&uses-sdk android:minSdkVersion="8" /&
&application android:icon="@drawable/icon" android:label="@string/app_name"&
&!-- android:theme="@android:style/Theme.NoTitleBar" 的意思是将系统默认的tag标签去掉,为咱们自己的标签空出位置--&
&activity android:name=".TagView"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
&intent-filter&
&action android:name="android.intent.action.MAIN" /&
&category android:name="android.intent.category.LAUNCHER" /&
&/intent-filter&
&/activity&
&!-- 在主配置文件中声明用于标签切换的3个activity,记住此处一定要声明,否则会出错
android:name="ArtistActivity"里面ArtistActivity前面是否有.都可以,你只需要保证该类是在manifest标签下package属性的包中即可。
&activity android:name="ArtistActivity"
android:label="@string/app_name"&&/activity&
&activity android:name="MusicActivity" android:label="@string/app_name"&&/activity&
&activity android:name="SportActivity"
android:label="@string/app_name"&&/activity&
&/application&
&/manifest&
一切都弄好之后,运行,就出现了最终效果。这里要注意,main.xml是一直都没有用到的哦。
废话连篇:
&&&&& 其实,利用TabHost布局与ListView有很多相似之处,系统也同样为他们提供了帮助类,TabHost-TabActivity& ListView-ListActivity .当我们的activity集成了这些类之后,一般在里面我们只需要整理绑定下数据就可以。
&&&&& 再次声明一下,代码中是存在setContentView方法的调用的,只不过因为我们集成了TabActivity,TabActivity的getTabHost方法中已经进行了实现而已。对用户隐藏了,并不代表没有。
&&&& 项目中为了简单易懂,我们只是在每个标签的内容部分添加了一个文本。实际上,我们完全可以在里面添加图片、视频等等。只要在相应的activity中实现就行了。我们可以看到,这种方式其实有很好的分层结构,activity与activity之间没有太多耦合。
&&&&& 可能一直到现在,有些朋友对TabActivity和ListActivity这种实现都特别的别扭。我这里就简单的说一下,实际上这其实是一种设计模式,模板模式。系统给你提供了一个实现了大部分内容的模板,然后你通过继承模板,去做修改(例如模板中有一个方法没有任何实现,你重写该方法并对其进行具体实现),让其符合你的要求。这就是模板模式的原理。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:897588次
积分:8591
积分:8591
排名:第1074名
原创:105篇
转载:11篇
评论:337条
(1)(1)(1)(1)(4)(1)(83)(24)android不用xml实现Tab效果(TabHost学习) -
- ITeye技术网站
博客分类:
public void initTabLayout()
TabHost tabHost = new TabHost( this ,null);//后面这个null一定要加,否则在addTab时会报资源找不到的错误
LinearLayout lineLayout = new LinearLayout( this );
lineLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
lineLayout.setOrientation( LinearLayout.VERTICAL );
lineLayout.setGravity( Gravity.TOP );
TabWidget tabWidget = new TabWidget( this );
tabWidget.setId( android.R.id.tabs );
//TabHost必须有一个ID为R.id.tabs的TabWidget,内部代码是根据此ID来打出TabWidget,
//详看TabHost的无参setup方法
tabWidget.setLayoutParams( new LinearLayout.LayoutParams( -1, -2) );
FrameLayout frameLayout = new FrameLayout( this );
frameLayout.setId( android.R.id.tabcontent );//TabHost必须有一个ID为R.id.tabContent的FrameLayout
frameLayout.setLayoutParams( new LinearLayout.LayoutParams( -1, -2) );
Button b1 = new Button( this );
b1.setId(123);
b1.setText(" tab button1 ");
Button b2 = new Button( this );
b2.setId(456);
b2.setText(" tab button2 ");
frameLayout.addView( b1 );
frameLayout.addView( b2 );
lineLayout.addView( tabWidget );
lineLayout.addView( frameLayout );
tabHost.addView( lineLayout );
Call setup() before adding tabs if loading TabHost using findViewById().
You do not need to call setup() after getTabHost()
//在添加tabs之前要调用,当然前提是TabHost不是通过getTabHost获取,也就是说不是通过TabActivity实现
tabHost.setup();
//不能直接new TabSpec
TabSpec deviceTab = tabHost.newTabSpec("Device");
deviceTab.setIndicator( "Device Tab" );
deviceTab.setContent( b1.getId() );
TabSpec softTab = tabHost.newTabSpec("Soft");
softTab.setIndicator( "Soft Tab" );
* 如果用TabHost tabHost = new TabHost( this );获取tabHost会报这种错
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
* at android.content.res.Resources.getValue(Resources.java:1013)
* at android.content.res.Resources.loadXmlResourceParser(Resources.java:2098)
* at android.content.res.Resources.getLayout(Resources.java:852)
* at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
* at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:543)
* at android.widget.TabHost.addTab(TabHost.java:223)
* 下面来分析原因:
TabHost 541行:
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tabIndicator = inflater.inflate(mTabLayoutId,
mTabWidget, // tab widget is the parent
false); // no inflate params
inflate会去查找mTabLayoutId定义的资源文件,那么mTabLayoutId又是怎么来的?
TabHost有两个构造器,TabHost( Context contex )里面是没有初始化mTabLayoutId,这样对象加载后它默认为0
再看TabHost 82行(再第二个构造器TabHost( Context context, AttributeSet attrs)中有对它进行初始化):
mTabLayoutId = a.getResourceId(R.styleable.TabWidget_tabLayout, 0);
a.recycle();
if (mTabLayoutId == 0) {
// In case the tabWidgetStyle does not inherit from Widget.TabWidget and tabLayout is
// not defined.
mTabLayoutId = R.layout.tab_indicator_
softTab.setContent( b2.getId() );
tabHost.addTab( deviceTab );
tabHost.addTab( softTab );
tabHost.setCurrentTab( 0 );
linearLayout.addView( tabHost );
浏览: 114736 次
来自: 天门
感谢 我终于找着源代码了!!!
xuke6677 写道您好,请问下我对无主键的表做全查询的时候 ...
您好,请问下我对无主键的表做全查询的时候,可以查出有多少条记录 ...
谢谢分享,学习啦
loveelwin 写道再补充一下,我说的对所有代码预编译是要 ...10137人阅读
上图为最终效果图
代码结构图
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:id="@+id/hometabs"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&!-- TabHost必须包含一个 TabWidget和一个FrameLayout--&
&TabHost android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
&LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&!-- TabWidget的id属性必须为 @android:id/tabs--&
&TabWidget android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"&
&/TabWidget&
&!-- FrameLayout的id属性必须为 @android:id/tabcontent--&
&FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&TextView android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/&
&TextView android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/&
&TextView android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/&
&/FrameLayout&
&/LinearLayout&
&/TabHost&
&/LinearLayout&
java代码如下
import android.app.A
import android.os.B
import android.widget.TabH
import android.widget.TabW
public class TagHostTest2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取TabHost对象
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
// 如果没有继承TabActivity时,通过该种方法加载启动tabHost
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一个标签",
getResources().getDrawable(R.drawable.icon)).setContent(
R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签")
.setContent(R.id.view3));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签")
.setContent(R.id.view2));
运行得到正确的结果。
废话连篇:这里需要注意的是
第一:布局文件的格式。以及TabWidget和FrameLayout的id属性值。
第二:TabWidget代表的是标签部分,FrameLayout代表的点击标签后看到的内容部分。FrameLayout里面声明的组件意为具备成为标签内容的资格,具体的还要在代码中具体指定。
你是否也想要这种结果呢。让标签在下部分显示
那么你只需要给main.xml进行下布局修改就可以了。
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:id="@+id/hometabs" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"&
&!-- TabHost必须包含一个 TabWidget和一个FrameLayout--&
&TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
android:layout_height="wrap_content"&
&LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"&
&!-- FrameLayout的id属性必须为 @android:id/tabcontent--&
&FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"&
&TextView android:id="@+id/view1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hello baby!"
&TextView android:id="@+id/view2" android:layout_width="fill_parent"
android:layout_height="fill_parent" /&
&TextView android:id="@+id/view3" android:layout_width="fill_parent"
android:layout_height="fill_parent" /&
&/FrameLayout&
&RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&!-- TabWidget的id属性必须为 @android:id/tabs--&
&TabWidget android:id="@android:id/tabs"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="0dp"
&/TabWidget&
&/RelativeLayout&
&/LinearLayout&
&/TabHost&
&/LinearLayout&
&&&&& 为了让标签和父容器底部持平,我们使用了android:layout_alignParentBottom="true",该属性只有在RelativeLayout布局中才会存在哦、这也是为什么我们将tabWidget放入一个RelativeLayout中的原因。
此外,在lineaerLayout布局中,TabWidget和FrameLayout的位置可是调换了哦。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:897590次
积分:8591
积分:8591
排名:第1074名
原创:105篇
转载:11篇
评论:337条
(1)(1)(1)(1)(4)(1)(83)(24)当前访客身份:游客 [
积跬步,至千里!!!!
:为什么手动切换一个page后才显示PagerTitleStrip...
:引用来自“算你狠”的评论 Context mContext=nul...
:Context mContext= public FlectionView(Co...
:怎么都没有R.layout.tab1 tab1
:多谢1楼提示
:非常感谢! 今天刚看pull解析搞不太懂,现在看完...
:引用来自“leeyongchao”的评论不是判断中断了吗...
:不是判断中断了吗,为什么会打印异常呢?
:很好的教程,学习了
今日访问:152
昨日访问:341
本周访问:4014
本月访问:4284
所有访问:116020
TabHost的使用
发表于2年前( 10:17)&&
阅读(1036)&|&评论()
0人收藏此文章,
TabHost的基本使用,主要是layout的声明要使用特定的id号,然后activity继承TabActivity即可。
&TabHost xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" &
&LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
&TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" &
&/TabWidget&
&FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" &
&LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" &
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="aa" /&
&/LinearLayout&
&LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" &
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="bb" /&
&/LinearLayout&
&/FrameLayout&
&/LinearLayout&
&/TabHost&
Main.java:
package com.app.
import android.app.TabA
import android.os.B
import android.view.V
import android.view.View.OnClickL
import android.widget.TabH
import android.widget.TabHost.OnTabChangeL
import android.widget.TabHost.TabS
import android.widget.TabW
public class Main extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = this.getTabHost();
TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("tab1")
.setContent(R.id.tab1);
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator("tab2")
.setContent(R.id.tab2);
tabHost.addTab(tab2);
实现效果:
当点击tabwidget的时候,若想注册事件监听器,可以使用:
1.tabHost.setOnTabChangedListener(new TabChangeListener(){
& & public void onTabChanged(String id)
&&&&&&&&{ &&&&&&&&}
这个传入的id,就是tabwidget的indicator,这里是"tab1","tab2";
2.调用tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){
更多开发者职位上
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读下次自动登录
现在的位置:
& 综合 & 正文
通过实现继承TabActivity并且实现TabContentFactory的方法创建选项卡的一点心得
1. 在onCreate方法中我们要通过TabHost的addTab方法添加选项卡,如果没有显示指定,系统会将第一个add方法添加进去的选项卡作为默认的选项卡
if (mCurrentTab == -1) {
setCurrentTab(0);
在setCurrentTab方法就回去调用
mCurrentView = spec.mContentStrategy.getContentView();这行,getContentView()方法回去调用
public View getContentView() {
if (mTabContent == null) {
mTabContent = mFactory.createTabContent(mTag.toString());
mTabContent.setVisibility(View.VISIBLE);
return mTabC
TabContentFactory是TabActivity提供给我们的一个接口,我们可以在createTabContent方法中创建我们自己的view
当再次执行addTab方法添加第二个,第三个选项卡的时候,都不会触发createTabContent方法了。再次的触发就是在每个选项卡第一次显示的时候。
当执行完createTabContent方法,即创建了tab中要显示的view之后,就回去调用onTabChanged方法,特别要注意的是当第一
次跳转到TabActivity的时候,并不会触发onTabChanged事件。当配合ListView使用TabActivity的时候,一般我们都
要异步的去给ListView的适配器提供数据,当onTabChanged事件触发的时候,适配器可能还没有值。(ptt项目中遇到)
比如一个TabHost中有三个选项卡A,B,C,当第一次跳转到此activity,我们依次点击A,B,CcreateTabContent()方法
会依次执行三次,执行的过程中,系统就会将创建的显示view存储起来,当点击C,B,A的时候,并不会去触发CcreateTabContent()方
法,系统会将已经存储的view取出来显示。
1. 如何求两个整数的百分比
int currentindex = 55;
int totalcount = 66;
NumberFormat nf = NumberFormat.getPercentInstance();
final String persent = nf.format(((float)currentindex/(float)totalcount));
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 安卓tabactivity 的文章

 

随机推荐