TabActivity的浅薄理解
来源:互联网 发布:cc域名注册 编辑:程序博客网 时间:2024/05/04 08:19
TabActivity在android中实现多Activity切换,效果和之前的文章ActivityGroup+ScrollView效果类似。在SDK文档中TabActivity的类继承结构如下:
java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity ↳ android.app.ActivityGroup ↳ android.app.TabActivity文档在介绍这个类的时候还用了很多Demo来讲解,可以去看看。
好,这里主要介绍TabActivity+TabHost配置实现多个Activity切换的效果,根据官网的Demo和网上资料,我写了一个小小的例子来说明这个类的使用
首先建立一个类继承TabActivity
package dxd.tabactivity.test;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class TabActivityTestActivity extends TabActivity {private TabHost m_tabHost; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_tabHost = getTabHost(); addOneTab(); addTwoTab(); addThreeTab(); } public void addOneTab(){ Intent intent = new Intent(); intent.setClass(TabActivityTestActivity.this, ActivityA.class); TabSpec spec = m_tabHost.newTabSpec("One"); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.tabspec1_layout, null); spec.setIndicator(view); spec.setContent(intent); m_tabHost.addTab(spec); } public void addTwoTab(){ Intent intent = new Intent(); intent.setClass(TabActivityTestActivity.this, ActivityB.class); TabSpec spec = m_tabHost.newTabSpec("Two"); spec.setIndicator(getString(R.string.two), getResources().getDrawable(R.drawable.icon)); spec.setContent(intent); m_tabHost.addTab(spec); } public void addThreeTab(){ Intent intent = new Intent(); intent.setClass(TabActivityTestActivity.this, ActivityC.class); TabSpec spec = m_tabHost.newTabSpec("Three"); spec.setIndicator(getString(R.string.three), null); spec.setContent(intent); m_tabHost.addTab(spec); } }由代码可以看出,是用TabHost下的一个子类TabSpec将这些按钮和Activity加入其中的,我加了3中风格的Tab进去,第一种是自己定义的布局文件,使用LayoutInflater将layout转换成View,第二种是加入了一个图片和文字,第三种只加入了文字。可以去看setIndicator这个方法,这里我罗列一下:
TabHost.TabSpec setIndicator(CharSequence label) // 只设置文字信息TabHost.TabSpec setIndicator(View view) // 设置一个View 这就很灵活了TabHost.TabSpec setIndicator(CharSequence label, Drawable icon) // 设置文字和图片,但是这个图文重叠了。求解啊,该怎么改?
即TabHost的id必须是android:id="@android:id/tabhost"
TabWidget的id必须是android:id="@android:id/tabs"
定义一个FrameLayout的id必须是android:id="@android:id/tabcontent"
见代码:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="1dip" android:paddingRight="1dip" android:paddingTop="4dip" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </TabHost>
另外,自己定义的布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabspec" android:layout_width="fill_parent" android:orientation="horizontal" android:background="#fff000" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28sp" android:text="是的" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28sp" android:text="@string/one" /></LinearLayo
- TabActivity的浅薄理解
- ActivityGroup的浅薄理解
- libuv的浅薄理解
- 对tracking的浅薄理解
- 我对AJAX的一点浅薄理解
- 关于网络编程相关的浅薄理解
- 关于spring AOP的浅薄理解
- 对于注解的浅薄理解,有待修正
- 对Spring的AOP的浅薄的理解
- 关于闭包的浅薄理解——笔记
- 对java String类型字符串的一些浅薄理解
- 19.Windows跨模块内存管理的浅薄理解
- 浅薄
- 才意识到我的浅薄.....
- fbo的个人浅薄了解
- 浅薄”绝不该是中国程序员的性格特征
- 浅薄的人生经历和思考
- 我浅薄的看《裸婚时代》
- poj1556
- 简单实用的分享
- 机器学习经典算法5-支持向量机SVM
- ruby protect_from_forgery ???????
- 从MicroVisonMV-8100上获取多幅图像
- TabActivity的浅薄理解
- GetModuleFileNameA函数和GetCurrentDirectoryA函数的应用
- h.264 ES流转PS流 包括头文件定义部分
- 正则表达式 简介
- 共享内存多次写入不同类型的数据测试代码
- Foundation-结构体
- ruby 正则表达式
- 著名技术博客收藏
- include&&require