package com.anyjoys.view.component;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import com.anyjoys.R;
/**
* Tab布局基類包括(底部菜單和頂部菜單的管理)
*
* @author summer
*
*/
public class TabLayoutActivity extends TabActivity {
private TabHost mTabHost;
private TextView textview_unread;
private View lastView; //記錄上次顯示視圖
public TabHost getmTabHost() {
return mTabHost;
}
private View view1;
public View getView1() {
return view1;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
this.mTabHost = (TabHost)findViewById(android.R.id.tabhost);
this.mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if( lastView!=null)
lastView.setVisibility(View.GONE);
FrameLayout frameLayout=mTabHost.getTabContentView();
frameLayout.setVisibility(View.VISIBLE);
lastView = frameLayout;
}
});
}
/**
* 填充底部菜單 注意所有參數一一對應
*
* @param menu_id
* 預先設定菜單ID
* @param top_icon_resid
* 菜單表徵圖資源檔數組
* @param str_resid
* 菜單文字資源檔數組
* @param intent
* 定義意圖
*/
public void setBottomMenuAndIntent(int[] menu_id, int[] top_icon_resid,
int[] str_resid, Intent[] intent) {
if (null != mTabHost) {
for (int j = 0; j < menu_id.length; j++) {
view1 = View.inflate(TabLayoutActivity.this, R.layout.tab,
null);// 功能表按鈕布局檔案
((ImageView) view1.findViewById(R.id.tab_imageview_icon))
.setImageResource(top_icon_resid[j]);
((TextView) view1.findViewById(R.id.tab_textview_title))
.setText(str_resid[j]);
view1.setBackgroundResource(R.drawable.bottom_btn_bg);
/* 初始化意圖 */
mTabHost.addTab(buildTabSpec(menu_id[j] + "", view1,
intent[j]));
}
}
}
/**
* 是否隱藏底部
*
* @param isvisibility
*/
public void hideBottomMenu(boolean isvisibility) {
if (isvisibility) {
this.mTabHost.getTabWidget().setVisibility(View.GONE);
} else {
this.mTabHost.getTabWidget().setVisibility(View.VISIBLE);
}
}
/**
* 設定底部背景圖片
*/
public void setTabBackground(int bg_resid){
this.mTabHost.getTabWidget().setBackgroundResource(bg_resid);
}
/**
* 初始化tab
*
* @param tag
* @param view
* @param content
* @return
*/
private TabHost.TabSpec buildTabSpec(String tag, View view,
final Intent content) {
return mTabHost.newTabSpec(tag).setIndicator(view)
.setContent(content);
}
/**
* 開始啟動意圖intent
*
* @param menu_id
*/
public void startIabIntent(int menu_id) {
this.mTabHost.setCurrentTabByTag(menu_id + "");
}
/**
* 必須先構造菜單再調用該方法,不然不顯示
* 菜單選項中訊息顯示
* @param postion 選擇顯示菜單位置 如果大於功能表項目預設為0
* @param bgimgres 提示資訊的背景表徵圖 可選 0
* @param num 顯示數量
*/
public void setTipStr(int postion,int bgimgres,int num){
TabWidget tab=getmTabHost().getTabWidget();
int count=tab.getChildCount();
if(count>0){
postion=postion>count?0:postion-1;
View menuview=tab.getChildAt(postion);
textview_unread=(TextView) menuview.findViewById(R.id.tab_textview_unread);
textview_unread.setVisibility(View.VISIBLE);
if(bgimgres!=0){
if(getResources().getDrawable(bgimgres) instanceof Drawable){
textview_unread.setBackgroundResource(bgimgres);
}
}
textview_unread.setText(num+"");
}
}
/**
* 隱藏菜單訊息
* @param postion 如果大於功能表項目預設為0
*/
public void hideTipStr(int postion){
TabWidget tab=getmTabHost().getTabWidget();
int count=tab.getChildCount();
if(count>0){
postion=postion>count?0:postion-1;
View menuview=tab.getChildAt(postion);
textview_unread=(TextView) menuview.findViewById(R.id.tab_textview_unread);
textview_unread.setVisibility(View.GONE);
}
}
}