
继承tabactivity并以activity布局
先查看下最终效果图:
再看下代码结构:
其中black.gif顾名思义就是一个黑背景图片,grey.gif就是一张灰色的背景图片
然后直接上代码:
ArtistActivity.java
package cn.com.tagvIEw; import androID.app.Activity; import androID.os.Bundle; import androID.Widget.TextVIEw; public class ArtistActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextVIEw textVIEw = new TextVIEw(this); // 该文档将会作为标签的内容进行显示 textVIEw.setText("艺术内容"); setContentVIEw(textVIEw); } } MusicActivity.java
package cn.com.tagvIEw; import androID.app.Activity; import androID.os.Bundle; import androID.Widget.TextVIEw; public class MusicActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextVIEw textVIEw = new TextVIEw(this); // 该文档将会作为标签的内容进行显示 textVIEw.setText("音乐内容"); setContentVIEw(textVIEw); } } SportActivity.java
package cn.com.tagvIEw; import androID.app.Activity; import androID.os.Bundle; import androID.Widget.TextVIEw; public class SportActivity extends Activity { @OverrIDe 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"?> <selector xmlns:androID="http://schemas.androID.com/apk/res/androID" > <item androID:drawable="@drawable/grey" androID:state_selected="true" ></item> <item androID:drawable="@drawable/black" ></item> </selector>
这里一定要注意ic_tab.xml文件的位置,是放在res/drawable文件夹下的。有些朋友说怎么没有这个文件夹啊,实际上大家看到了我将它放在了drawable-hdpi中了,实际上drawable-hdpi、drawable-ldpi、drawable-mdpi三个文件夹都属于drawable文件夹的哦。该文件它规定了,当标签获得焦点和失去焦点时,标签上显示什么图片。
例如本例中,就是当state_selected="true"(当标签被选中时),显示@drawable/grey指定的资源图片。当未被选中时,显示@drawable/black指定的资源图片。
tagVIEw.java代码:
package cn.com.tagvIEw; import androID.app.tabactivity; import androID.content.Intent; import androID.content.res.Resources; import androID.os.Bundle; import androID.Widget.TabHost; /** * @author chenzheng_Java * @description 注意,该类一定要继承tabactivity */ public class TagVIEw extends tabactivity { @OverrIDe 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.TabSpec spec; // 声明一个意图,该意图告诉我们,下一个跳转到的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="http://schemas.androID.com/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这种实现都特别的别扭。我这里就简单的说一下,实际上这其实是一种设计模式,模板模式。系统给你提供了一个实现了大部分内容的模板,然后你通过继承模板,去做修改(例如模板中有一个方法没有任何实现,你重写该方法并对其进行具体实现),让其符合你的要求。这就是模板模式的原理。
继承tabactivity并以布局文件进行布局
然后再来看以XML布局文件进行布局的方法,先上效果图:
上面的是最终效果图。
代码结构如下。
main.xml代码:
<?xml version="1.0" enCoding="utf-8"?> <!-- 该布局文件定义了标签的内容部分,该布局文件一定要以FrameLayout为根元素 --> <FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <!-- 第一个标签内容 --> <linearLayout androID:ID="@+ID/Widget_layout_Blue" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <EditText androID:ID="@+ID/Widget34" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="EditText" androID:textSize="18sp"> </EditText> <button androID:ID="@+ID/Widget30" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="button"> </button> </linearLayout> <!-- 第二个标签内容 AnalogClock为钟表组件--> <linearLayout androID:ID="@+ID/Widget_layout_red" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <AnalogClock androID:ID="@+ID/Widget36" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> </AnalogClock> </linearLayout> <!-- 第三个标签内容 Radiobutton必须在RadioGroup中哦 --> <linearLayout androID:ID="@+ID/Widget_layout_green" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical"> <RadioGroup androID:ID="@+ID/Widget43" androID:layout_wIDth="166px" androID:layout_height="98px" androID:orIEntation="vertical"> <Radiobutton androID:ID="@+ID/Widget44" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton"> </Radiobutton> <Radiobutton androID:ID="@+ID/Widget45" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton"> </Radiobutton> </RadioGroup> </linearLayout> </FrameLayout>
TagHostTest.java的代码:
package cn.com.tagHost.test; import androID.app.tabactivity; import androID.graphics.color; import androID.os.Bundle; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEwGroup; import androID.Widget.TabHost; public class TagHostTest extends tabactivity { private TabHost myTabhost; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myTabhost = this.getTabHost(); /** * inflate(int resource,VIEwGroup root,boolean attachToRoot) * resource 很显然是一个资源索引ID * 当attachToRoot为true时,root代表一个可放置于容器中的组件 * 当attachToRoot为false时,root仅代表一个存储值的对象 * 该方法的意思是,将根据R.layout.main生成的标签VIEw,添加到由myTabhost.getTabContentVIEw()获得的父容器中 * LayoutInflater类的inflate方法中有如下片段 * if (root != null && attachToRoot) { root.addVIEw(temp,params); } 其中temp是根据resource指定的资源生成的一个和标签有关的vIEw */ LayoutInflater.from(this).inflate(R.layout.main,myTabhost.getTabContentVIEw(),true); myTabhost.setBackgroundcolor(color.argb(150,22,70,150)); myTabhost.addTab(myTabhost.newTabSpec("One") .setIndicator("A").setContent(R.ID.Widget_layout_Blue)); myTabhost.addTab(myTabhost.newTabSpec("Two") .setIndicator("B",getResources().getDrawable(R.drawable.icon)) .setContent(R.ID.Widget_layout_green)); myTabhost.addTab(myTabhost.newTabSpec("Three") .setIndicator("C",getResources().getDrawable(R.drawable.icon)) .setContent(R.ID.Widget_layout_red)); } } 这种方法实现起来比较简单,看看我们都做了些什么。
第一步:定义标签内容部分的布局文件,该布局文件必须以FrameLayout为根节点。
第二步:让activity继承tabactivity,然后实现自己的代码。
以上是内存溢出为你收集整理的Android应用中使用TabHost组件继承TabActivity的布局方法全部内容,希望文章能够帮你解决Android应用中使用TabHost组件继承TabActivity的布局方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)