Android 常用UI控件之TabHost(2)简单示例

1,布局

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frgmt_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" > <TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="true" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs"> <!-- 注意此句,如果没有此句,当tab1,tab2...过高时,会TabWidget会盖住 -->
<!-- tab1,用include可以引用别处的layout -->
<include
android:id="@+id/tab_weixin"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/tab_weixin_layout" />
<!-- tab2,用include可以引用别处的layout -->
<include
android:id="@+id/tab_contacts"
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/tab_contacts_layout" />
<!-- tab3,用include可以引用别处的layout -->
<include
android:id="@+id/tab_discovery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/tab_discovery_layout" />
<!-- tab4,用include可以引用别处的layout -->
<include
android:id="@+id/tab_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/tab_me_layout" />
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E0E0E0"
android:layout_alignParentBottom="true"
android:showDividers="none" >
</TabWidget>
</RelativeLayout>
</TabHost>
</FrameLayout>

2,在代码中初始化tab栏

     void initTabHostTabs(LayoutInflater inflater){

         tabHost.setup();

         ScaleDrawable scale ;
TabSpec tab = tabHost.newTabSpec("weixin");
View tabView1 = inflater.inflate(R.layout.tab_indicator, null, false);
TextView tabTitle = (TextView) tabView1.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_weixin);
ImageView tabIcon = (ImageView) tabView1.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_weixin_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView1);
tab.setContent(R.id.tab_weixin);
tabHost.addTab(tab); tab = tabHost.newTabSpec("contacts");
View tabView2 = inflater.inflate(R.layout.tab_indicator, null, false);
tabTitle = (TextView) tabView2.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_contacts);
tabIcon = (ImageView) tabView2.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_contacts_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView2);
tab.setContent(R.id.tab_contacts);
tabHost.addTab(tab); tab = tabHost.newTabSpec("discovery");
View tabView3 = inflater.inflate(R.layout.tab_indicator, null, false);
tabTitle = (TextView) tabView3.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_discovery);
tabIcon = (ImageView) tabView3.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_discovery_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView3);
tab.setContent(R.id.tab_discovery);
tabHost.addTab(tab); tab = tabHost.newTabSpec("me");
View tabView4 = inflater.inflate(R.layout.tab_indicator, null, false);
tabTitle = (TextView) tabView4.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_me);
tabIcon = (ImageView) tabView4.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_me_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView4);
tab.setContent(R.id.tab_me);
tabHost.addTab(tab); /* 测试可以向右划动tab
for (int i = 0; i < 5; i++) {
tab = tabHost.newTabSpec("more" + i);
tab.setIndicator("more" + i);
tab.setContent(R.id.tab_me);
tabHost.addTab(tab);
}
*/
}
上一篇:Python属性、方法和类管理系列之----描述符类


下一篇:C#实现union以及lock的使用