萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 解決android tabhost 個別版本視圖重疊問題

解決android tabhost 個別版本視圖重疊問題

   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);

  }

  }

  }

copyright © 萬盛學電腦網 all rights reserved