使用TabHost可以通过继承TabHost的方式,也可以通过继承ActivityGroup的方式。
通过extends TabHost比较简单,这里就不介绍了,以下是介绍通过extends ActivityGroup的方式使用TabHost,也就是通过findViewById方式创建TabHost对象的方式,这种方式大家可能会遇到以下错误提示:
1、java.lang.IllegalStateException: Did you forget to call 'public void setup,原因是未使用setup()
2、Android Exception: Did you forget to call 'public void setup (LocalActivityManager activityGroup),原因是也是未正确使用setup()
3、java.lang.IllegalStateException: Activities can't be added until the containing group has been created.,原因是未继承ActivityGroup,如果只是extends Activity是不行的,必须extends ActivityGroup
4、其它错误......
原因都是因为没有按正确的方法调用TabHost,附上代码:
1、AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.shuxiang.tabhostsample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Tab1" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="org.shuxiang.tabhostsample.Tab1" />
</intent-filter>
</activity>
<activity android:name="Tab2" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="org.shuxiang.tabhostsample.Tab2" />
</intent-filter>
</activity>
<activity android:name="Tab3" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="org.shuxiang.tabhostsample.Tab3" />
</intent-filter>
</activity>
</application>
</manifest>
layout下的布局文件:
2、tabhost.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
3、tabhost1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"
android:padding="10px"/>
</LinearLayout>
4、tabhost2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:padding="10px"/>
</LinearLayout>
5、tabhost3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"
android:padding="10px"/>
</LinearLayout>
value下的文件:
6、strings.xml
<resources>
<string name="app_name">TabHostSample</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
类文件:
7、MainActivity.java
package org.shuxiang.tabhostsample;
import android.os.Bundle;
import android.app.ActivityGroup;
import android.content.Intent;
import android.widget.TabHost;
public class MainActivity extends ActivityGroup
{
private String tabHost1 = "A";
private String tabHost2 = "B";
private String tabHost3 = "C";
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup(getLocalActivityManager());
tabHost.setCurrentTab(0);
tabHost.addTab(tabHost.newTabSpec(tabHost1)
.setIndicator(tabHost1).
setContent(new Intent(MainActivity.this, Tab1.class)));
tabHost.addTab(tabHost.newTabSpec(tabHost2)
.setIndicator(tabHost2)
.setContent(new Intent(MainActivity.this, Tab2.class)));
tabHost.addTab(tabHost.newTabSpec(tabHost3)
.setIndicator(tabHost3)
.setContent(new Intent(MainActivity.this, Tab3.class)));
}
}
8、Tab1.java
package org.shuxiang.tabhostsample;
import android.app.Activity;
import android.os.Bundle;
public class Tab1 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost1);
}
}
9、Tab2.java
package org.shuxiang.tabhostsample;
import android.app.Activity;
import android.os.Bundle;
public class Tab2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost2);
}
}
10、Tab3.java
package org.shuxiang.tabhostsample;
import android.app.Activity;
import android.os.Bundle;
public class Tab3 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost3);
}
}
附上效果图:
分享到:
相关推荐
简单实现Android的findviewbyid IOC框架
一个根据布局文件自动生成findViewById代码的android studio插件
Android注解省去findviewbyid,在Android开发的过程中有很多控件都要findviewbyid的,这样太麻烦了。
如果继承TabActivity,并且通过getTabHost()方法来获取TabHost,那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent,如果继承Activity,可以通过findViewById来获取这三个组件...
一个简单的注解实现 findViewById 和 setOnClickListener 的例子
Android开发中经常使用 findviewbyid来找控件, 这个工具类可以方便的生成代码,减少代码的书写。
这是可以让你告别繁杂的findViewById的一个实例,通过注解的方式来实现。
主要介绍了Android中findViewById获取控件返回为空问题怎么解决的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
创建全新的视图将满足我们独特的UI需求。 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图。
主要介绍了Android中findViewById返回为空null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Android编程设置TabHost当中字体的方法。... TextView tv=(TextView)tw.getChildAt(i).findViewById(android.R.id.title); tv.setGravity(BIND_AUTO_CREATE); tv.setPadding(10, 10, 10
vq id R id imageView1 image android R drawable btn plus ; 自动转换成子类 textview imageView listView gridView EditText et vq id R id editText1 getView ; 第二次查找id的时候 没有从布局文件中去找 而是...
Android技术知识点:如何使用视图绑定来消除findViewById()
Android Studio 插件 传播:heart: 关于 在GitHub上看到Android-Studio-Plugins 项目并有了想法,这里想做一份比较全面的Android Studio开发插件整理,希望可以帮助更多的 Android 开发者来加快开发速度,也希望...
开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,解决方法如下: 代码如下:TabWidget tw = tabHost.getTabWidget(); for (int i = 0; i < tw.getChildCount(); i++) { TextView tv=...
为了减少频繁的调用findViewById(R.id...),可以采用一些注入框架,可以简化自己的代码,让你更专注于实际的功能开发,butterknife就是这样的一个框架.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,...
从 Android Studio 3.6 开始,视图绑定能够通过生成绑定对象来替代 findViewById,从而可以帮您简化代码、移除 bug,并且从 findViewById 的模版代码中解脱出来,今天通过本文给大家介绍使用视图绑定替代 ...
主要介绍了AndroidGUI27中findViewById返回null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下