WEB开发网
开发学院手机开发Android 开发 解决android tabhost 个别版本视图重叠问题 阅读

解决android tabhost 个别版本视图重叠问题

 2013-05-15 13:12:18 来源:开发学院   
核心提示:package com.anyjoys.view.component;import android.app.TabActivity;import android.content.Intent;import android.graphics.drawable.Drawable;import android.os.Bund
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);
		}
	}
}

Tags:解决 android tabhost

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接