Android应用中使用TabHost组件继承TabActivity的布局方法

继承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,然后实现自己的代码。

时间: 2024-07-31 22:28:02

Android应用中使用TabHost组件继承TabActivity的布局方法的相关文章

Android应用中使用TabHost组件继承TabActivity的布局方法_Android

继承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 ArtistA

详解Android应用中使用TabHost组件进行布局的基本方法_Android

TabHost布局文件 我们先来了解一下布局文件的基本内容:1. 根标签及id 设置Android自带id : XML布局文件中, 可以使用 标签设置, 其中的id 需要引用 android的自带id : android:id=@android:id/tabhost ; getHost()获取前提 : 设置了该id之后, 在Activity界面可以使用 getHost(), 获取这个TabHost 视图对象; 示例 : 复制代码 代码如下: <tabhost android:id="@an

Android开发中Activity创建跳转及传值的方法_Android

在Android系统的江湖中有四大组件:活动(Activity), 服务(Service), 广播接收器(Broadcast Reciver)和内容提供者(Content Provider). 今天所介绍的就是Android开发中的四大组件之一:Activity,其他那三大组件以后再进行介绍.说道Android中的Activity,如果你做过iOS开发的话,Activity类似于iOS中的ViewController(视图控制器).在应用中能看到的东西都是放在活动中的.活动是安卓开发比较重要的东

Android开发中RecyclerView模仿探探左右滑动布局功能

我在此基础上优化了部分代码, 添加了滑动回调, 可自定义性更强. 并且添加了点击按钮左右滑动的功能. 据说无图都不敢发文章了. 看图: 1:这种功能, 首先需要自己管理布局 继承 RecyclerView.LayoutManager , 显示自己管理布局, 比如最多显示4个view, 并且都是居中显示. 底部的View还需要进行缩放,平移操作. public class OverLayCardLayoutManager extends RecyclerView.LayoutManager { p

android studio-如何将Android Studio 中的项目打包成jar文件,求方法

问题描述 如何将Android Studio 中的项目打包成jar文件,求方法 如题,求能用的方法,试了好多百度的方法,不怎么好用啊,越详细越好 解决方案 AS 生成Jar包 在Gradle中添加任务声明 def makeJar(String target, String classDir) { exec { executable "jar" //调用jar args "cvf", target args "-C", classDir args

Android开发中在TableView上添加悬浮按钮的方法_Android

如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬浮按钮贴在view的最顶层 2.使用window 首先看一下最终的效果,在tableViewController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动 首先介绍上面的第一种方法: 1)创建tableview和底部按钮的属性 //屏幕宽 #define kScreenW [UIScreen

Android开发中在TableView上添加悬浮按钮的方法

如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬浮按钮贴在view的最顶层 2.使用window 首先看一下最终的效果,在tableViewController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动 首先介绍上面的第一种方法: 1)创建tableview和底部按钮的属性 //屏幕宽 #define kScreenW [UIScreen

Android开发中使用WebView控件浏览网页的方法详解

本文实例讲述了Android开发中使用WebView控件浏览网页的方法.分享给大家供大家参考,具体如下: 项目中遇到数学展示问题,常规的Textview显示处理不了数学公式,利用图片生成对服务器又产生较大压力,经过查询,可以通过webview加载JS实现.IOS同样的方法也可实现,但JS渲染效率远高于安卓.对Webview做下总结. 1.WebView 在使用WebView控件时,首先需要在xml布局文件中定义一个WebView控件,定义的方法如下: <WebView android:id=&quo

通过实例简单讲解Android App中的Activity组件_Android

Activity是Android应用中,最直接与用户接触的组件,它负责加载View组件,使其展现给用户,并保持与用户的交互.所有的Activity组件均需要继承Activity类,这是一个Content的间接子类,包装了一些Activity的基本特性. View组件是所有UI组件.容器组件的基类,也就是说,它可以是一个布局容器,也可以是一个布局容器内的基本UI组件.View组件一般通过XML布局资源文件定义,同时Android系统也对这些View组件提供了对应的实现类.如果需要通过某个Activ