当前位置:操作系统 > 安卓/Android >>

Android TabHost用法详解

最近研究了一下Contacts源码,仿照上面自己写了一个TabHostTest程序,现整理如下:

main.xml布局文件:

[html]
<?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"> 
 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
        /> 
 
        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_width="match_parent" 
            android:layout_height="0dip" 
            android:layout_weight="1" 
        /> 
    </LinearLayout> 
</TabHost> 

inner.xml文件:

[html]
<?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"> 
 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="0dip" 
            android:layout_weight="1" 
        /> 
         
        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
        /> 
 
 
    </LinearLayout> 
</TabHost> 
Main.java (主Activity类):

[java]
package com.android.test; 
 
import android.app.Activity; 
import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.provider.CallLog.Calls; 
import android.provider.Contacts.Intents.UI; 
import android.view.Window; 
import android.widget.TabHost; 
 
public class Main extends TabActivity implements TabHost.OnTabChangeListener { 
    private static final int TAB_INDEX_DIALER = 0; 
    private static final int TAB_INDEX_CALL_LOG = 1; 
    private static final int TAB_INDEX_CONTACTS = 2; 
    private static final int TAB_INDEX_FAVORITES = 3; 
 
    private TabHost mTabHost; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        final Intent intent = getIntent(); 
 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
         
        setContentView(R.layout.main); 
 
        mTabHost = getTabHost(); 
        mTabHost.setOnTabChangedListener(this); 
 
        // Setup the tabs 
        setupDialerTab(); 
        setupCallLogTab(); 
        setupContactsTab(); 
        setupFavoritesTab(); 
 
        setCurrentTab(intent); 
    } 
 
    public void onTabChanged(String tabId) { 
         Activity activity = getLocalActivityManager().getActivity(tabId); 
            if (activity != null) { 
                activity.onWindowFocusChanged(true); 
            } 
    } 
     private void setupCallLogTab() { 
            // Force the class since overriding tab entries doesn't work 
            Intent intent = new Intent("com.android.phone.action.RECENT_CALLS"); 
 
            intent.setClass(this, Inner.class); 
            mTabHost.addTab(mTabHost.newTabSpec("call_log") 
                    .setIndicator("通话记录", 
                            getResources().getDrawable(R.drawable.ic_tab_unselected_recent)) 
                    .setContent(intent)); 
        } 
 &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,