最近几天一直在研究TabHost,觉得系统的Tabhost的又大又丑,所以一直想定义和新浪微博等客户端一样好看的Tabhost .通过找一些博客终于学会了怎样做一些好看的TabHost。
学会了之后首先就来个高仿新浪微博底部的Tabhost...... 直接下apk解压取得素材
先贴出效果吧
源代码如下:(没继承TabActivity)
1建立一个 tabhost
<?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:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab5"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
2建立tabwight.xml ,通过它来改变系统tabwight的外观
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/bg"
android:paddingLeft="5dip"
android:paddingRight="5dip" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/tab_icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tab_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="12dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
java代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class WeiboTabHostActivity extends Activity {
String[] title = new String[]{"首页","消息","好友","广场","更多"};
View homeTab,messageTab,infoTab,searchTab,moreTab;
View[] tabs = new View[]{homeTab,messageTab,infoTab,searchTab,moreTab};
int[] tabIds = new int[]{R.id.tab1,R.id.tab2,R.id.tab3,R.id.tab4,R.id.tab5};
int[] drawables=new int[]{R.drawable.home,R.drawable.message,R.drawable.info,R.drawable.search,R.drawable.more}; //在drawable文件下给图片设的背景,其实就是一个selector
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup(); //Call setup() before adding tabs if loading TabHost using findViewById().
for(int i=0;i<tabs.length;i++){
tabs[i] = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, null);
ImageView icon=(ImageView) tabs[i].findViewById(R.id.tab_icon);
icon.setBackgroundResource(drawables[i]);
TextView text = (TextView) tabs[i].findViewById(R.id.tab_label);
text.setText(title[i]);
tabHost.addTab(tabHost.newTabSpec("").setIndicator(tabs[i]).setContent(tabIds[i]));
}
tabHost.setCurrentTab(2);
}
}
最近在做一个项目,需要定义一个tabhost每个标签的内容是一个Activity
我的源代码如下:(和上面定义的那个差不多)
public class TabActivity extends Activity{
private Intent[] intents;
View graphicTab,earTab,applyTab,coolTab;
View[] tabs = new View[]{graphicTab,earTab,applyTab,coolTab};
private int drawables[] =new int[]{
R.drawable.tabhost_1,R.drawable.tabhost_2,
R.drawable.tabhost_3,R.drawable.tabhost_4
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.tabhost);
intents=new Intent[]{
new Intent(this,GraphicActivity.class),
new Intent(this,EARActivity.class),
new Intent(this,ApplyActivity.class),
new Intent(this,CoolWebActivity.class)
};
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup();
for(int i=0;i<intents.length;i++){
tabs[i] = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, null);
ImageView icon=(ImageView)tabs[i].findViewById(R.id.tab_icon);
icon.setBackgroundResource(drawables[i]);
tabHost.addTab(tabHost.newTabSpec("").setIndicator(tabs[i]).setContent(intents[i]));
}
}
}
但是当我写完循环一运行就报错了, 说Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
最后百度,在一个全英文的网站里找到了答案
这个网址在这
原来在那里面有个人和我写的一样,同样是继承Activity,通过Id找到TabHost,然后setUp();
最后发现要这样写才行
public class MainActivity extends ActivityGroup { ... }
tabHost.setup(this.getLocalActivityManager());
这个网址在这http://stackoverflow.com/questions/3272500/android-exception-did-you-forget-to-call-public-void-setup-localactivitymanag
虽然问题是解决了但是还是有点不明白这setup为什么要这样写。。。没办法只好百度谁叫百度是我最好的老师呢!又是一篇博客让我真正的明白为什么不继承TabActivity为什么一定要setup一下。原来这个setup的作用是主要是初始化了tabhost的两个实例变量,这里告诉了我们为什么Tabwight,和Framelayout这两个标签的id必须使用系统定义的id的原因
我的tabhost里的内容是几个activity所以要传入activityManager对象这也合乎情理吧。。。。关于这个Tabhost的博客在这http://hi.baidu.com/z_hongc/blog/item/61cd77f07d551cbda50f522c.html
写得非常详细值得一看。。。
本人第二篇博客,喜欢的支持下,有望能得到大家的鼓励呀....