读书人

android tabhost嵌套运用 详解

发布时间: 2012-08-28 12:37:01 作者: rapoo

android tabhost嵌套使用 详解

废话不多说 上代码。还是很简单的。

父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="wrap_content" >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_below="@android:id/tabs" >        </FrameLayout>        <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_below="@id/tab_top"            android:layout_height="wrap_content" >        </TabWidget>    </RelativeLayout></TabHost>


子tab的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="wrap_content" >        <LinearLayout            android:id="@+id/tab_top"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_alignParentTop="true"            android:background="@drawable/background_login" >        </LinearLayout>        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_above="@android:id/tabs"            android:layout_below="@id/tab_top" >        </FrameLayout>        <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true" >        </TabWidget>    </RelativeLayout></TabHost>


为了区别两个tab。父tab的 tabhost id是 :android:id/tabhost 子tab的tabhost id 是 @+id/tabhost。

有什么不一样呢。等会就知道了。

父tab的activity:

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_datetab);TabHost tabHost= getTabHost();int height =30;TabWidget tabWidget = tabHost.getTabWidget();        tabHost.addTab(tabHost.newTabSpec("年").setIndicator("年").setContent(new Intent(this, ActTabMonth.class)));          tabHost.addTab(tabHost.newTabSpec("月").setIndicator("月").setContent(new Intent(this, ActTabDay.class)));          tabHost.addTab(tabHost.newTabSpec("日").setIndicator("日").setContent(new Intent(this, ActTabHour.class)));                for (int i =0; i < tabWidget.getChildCount(); i++) {                                    /**设置高度、宽度,由于宽度设置为fill_parent,在此对它没效果 */           tabWidget.getChildAt(i).getLayoutParams().height = height;   //            tabWidget.getChildAt(i).getLayoutParams().width = width;               /**设置tab中标题文字的颜色,不然默认为黑色 */                final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);                tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));           }


子tab的activity

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_tab);        setSpec();    }public void setSpec(){        TabHost tabHost= (TabHost)findViewById(R.id.tabhost);//        tabHost.setup();       
tabHost.setup(this.getLocalActivityManager());
tabHost.addTab(tabHost.newTabSpec("去电").setIndicator("去电",getResources().getDrawable(R.drawable.go)).setContent(new Intent(this, ActGoCall_day.class))); tabHost.addTab(tabHost.newTabSpec("来电").setIndicator("来电",getResources().getDrawable(R.drawable.go)).setContent(new Intent(this, ActComeCall_day.class))); tabHost.addTab(tabHost.newTabSpec("未接").setIndicator("未接",getResources().getDrawable(R.drawable.go)).setContent(new Intent(this, ActMissEdCall_day.class))); tabHost.addTab(tabHost.newTabSpec("新增客户").setIndicator("新增客户",getResources().getDrawable(R.drawable.go)).setContent(new Intent(this, ActContactNum_day.class)));}

在加载tabhost的时候如果用 getTabHost() 就需要id = android:id/tabhost;

是不是很简单呢。

还有就是子tab一定得是继承activityGroup 而且一定得需要这两句

 public class ActTabDay extends ActivityGroup


tabHost.setup(this.getLocalActivityManager());


第二句 需要在给TabHost 获取id之后马上进行实例化。

注意事项:如果在子tab里面加入的 activity 有dialog 需要将dialog改成

AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent()) 


这样即可。

读书人网 >操作系统

热点推荐