簡單解決android tabhost 有的版本,視圖重疊問題

來源:互聯網
上載者:User

[java]
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); 
        } 
    } 

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.