android点滴—TabHost实现
来源:互联网 发布:计算机编程艺术 pdf 编辑:程序博客网 时间:2024/05/29 17:39
一、TabHost实现方式有两种
1、直接让一个Activity程序继承TabActivity类;
2、利用findViewById()方法取得TabHost组件,进行若干设置。
二、继承TabActivity
1、tab显示上方
xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</FrameLayout>
code:
public class MovieActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost(); //通过TabActivity获取TabHost
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);
Intent intent1 = new Intent(this,MyListView.class);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("首页",getResources().getDrawable(R.drawable.home_b))
.setContent(intent1));
........
2、tab显示下方
xml:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- TabWidget组件id值不可变 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" ><!-- 控制上部还是下部显示 -->
</TabWidget>
<!-- FrameLayout布局,id值不可变 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@android:id/tabs" ><!-- 控制上部还是下部显示 -->
</FrameLayout>
</RelativeLayout>
</TabHost>
code:
public class MovieActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost(); //通过TabActivity获取TabHost
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);
Intent intent1 = new Intent(this,MyListView.class);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("首页",getResources().getDrawable(R.drawable.home_b))
.setContent(intent1));
........
三、findViewById()获取TabHost
1、
xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- TabWidget组件id值不可变 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" ><!-- 控制上部还是下部显示 -->
</TabWidget>
<!-- FrameLayout布局,id值不可变 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@android:id/tabs" ><!-- 控制上部还是下部显示 -->
</FrameLayout>
</RelativeLayout>
</TabHost>
code:
public class MovieActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TabHost tabHost = getTabHost();
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
LocalActivityManager mLocalActivityManager =new LocalActivityManager(this,false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
//tabHost.setup();
//LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), false);
Intent intent1 = new Intent(this,MyListView.class);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("首页",getResources().getDrawable(R.drawable.home_b))
.setContent(intent1));
......
四:说明
TabActivity 继承自 ActivityGroup,而ActivityGroup的主要作用是创建一个LocalActivityManager,然后把activity的onCreate等事件传递给LocalActivityManager来处理。
TabActivity真正重要的包括三部分:TabHost,TabWidget,LocalActivityManager。
当没有选择系统tabhost默认id时,而是自己定义的id,必须使用findViewById();setup()或者setup(LocalActivityManager).如果后面使用了setContent(Intent)即setContent参数是一个实例化的Intent时,必须使用setup(LocalActivityManager)。
setup方法主要是初始化了TabConnect和TabWidget
示例中setContent使用的是intent,也可以在配置文件里直接定义好布局,设置成view。
- android点滴—TabHost实现
- android tabhost Intent实现
- TabHost—多种实现
- android之实现底部TabHost
- Android之Fragment实现TabHost
- android之实现底部TabHost
- Android:TabHost实现Tab切换
- Android:TabHost实现Tab切换
- tabhost实现android菜单切换
- Android:TabHost实现Tab切换
- Android:TabHost实现Tab切换
- android开发-tabhost实现分页
- Android-TabHost实现Tab切换
- android Tabhost部件(四)--其他类似TabHost实现
- Android--TabHost的两种实现方法
- Android TabHost +Fragment 实现底部菜单栏
- 13-7-5 android Tabhost功能实现
- Android TabHost +Fragment 实现底部菜单栏 .
- 中国联合通信公司SGIP协议
- SpringMVC入门了解
- TinyXML:一个优秀的C++ XML解析器
- 第一篇博客:work harder for you
- 在面试时怎么回答java垃圾回收机制
- android点滴—TabHost实现
- HTML5
- 李小璐晒全家福 贾乃亮赤膊女儿可爱(图)
- Vim编辑器使用技巧
- HDU 1003 Max Sum 求最大连续和
- ASP.NET基础 HttpHandler
- iOS5:[UIDevice uniqueIdentifier]的替代方案
- 是的那是假的是说不定还是是哪毒神殿
- javascript正则表达式详解(中级篇)