Android项目--tabhost

时间:2021-06-30 21:31:22

所有牵扯到自定义布局的layout中尽量用RelativeLayout

在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的。通过tabhost来做。

1.布局

a) 直接用TabHost作为布局框架

b) 代码:

<?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="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="" /> <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_weight="0.0"
android:background="@drawable/contact_tabhost" >
</TabWidget> </RelativeLayout> </TabHost>

FrameLayout中就是填充需要的布局

TabWidge是切换的响应式按钮,两者有着关联性的作用

2.设计

a) 像QQ,微信这样的客户端,应该也是tabhost做的吧。简单,明了,不用费太多的事,就能在一个布局框架中,创建出无数个布局。可同,可异

b) 定义一个ContactActivity extends TabActivity

c) 代码:

private void createTab(int img, String text, Intent intent) {
mTabHost.addTab(mTabHost.newTabSpec(text)
.setIndicator(createTabView(img, text)).setContent(intent));
} private View createTabView(int img, String text) {
View view = LayoutInflater.from(this).inflate(
R.layout.contact_tab_indicator, null);
ImageView iv = (ImageView) view.findViewById(R.id.iv_tab);
TextView tv = (TextView) view.findViewById(R.id.tv_tab);
iv.setImageResource(img);
tv.setText(text);
return view;
}

3.功能

a) 需要三个布局来填充

b) 代码:

mTabHost = getTabHost();
Intent intent1 = new Intent(this, ContactListActivity.class);
createTab(R.drawable.contact, "通讯录", intent1); Intent intent2 = new Intent(this, ContactCalllog.class);
createTab(R.drawable.logcall, "通话记录", intent2); Intent intent3 = new Intent(this, ContactMess.class);
createTab(R.drawable.message, "短信", intent3); mTabHost.setCurrentTab();