`
yq135314
  • 浏览: 253193 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Tabhost与popupwindow

 
阅读更多

软件应用中,常看到一种效果是在点击更多时,弹出一更多的弹出框,其效果截图如下:

 

 

 

关键代码如下:

 

其中MenuView是自定义的一视图,在该视图中,主要是进行图片与文字的布局,以及处理弹出popupwindow弹框

 

	intent = new Intent(this, Test2Activity.class);
		spec = mTabHost
				.newTabSpec("Test3")
				.setIndicator(
						new MenuView(this, "更多", res
								.getDrawable(R.drawable.pic_more)))
				.setContent(intent);
		mTabHost.addTab(spec);
		mTabHost.setOnTabChangedListener(this);

 

 

TabChangeListener事件的处理如下:

 

	@Override
	public void onTabChanged(String tabId) {
		if(TextUtils.equals(tabId, "Test3")) {
			View v = mTabHost.getCurrentTabView();
			//设置为空的目的使得事件不再向下传播,即不会响tabchange事件
			v.setOnFocusChangeListener(null);
		}
	}
 

 

最后是MenuView的实现:

public class MenuView extends LinearLayout implements OnItemClickListener{
	private PopupWindow popuwindWindow;
	private ArrayList<HashMap<String, ?>> datas = null;
	private Context context;

	 public MenuView(Context context,String label, Drawable icon) {
	        super(context); 
	        this.context = context;
			datas = new ArrayList<HashMap<String, ?>>();
			init$Views();
	        this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	                LayoutParams.WRAP_CONTENT, (float) 1));
	        setGravity(Gravity.CENTER);
	        setOrientation(LinearLayout.VERTICAL);
	        ImageView img = new ImageView(context);
	        img.setImageDrawable(icon);
	        addView(img);
	        TextView tv = new TextView(context);
	        tv.setText(label);
	        tv.setGravity(Gravity.CENTER);
	        tv.setBackgroundColor(Color.TRANSPARENT);
	        tv.setTextColor(Color.WHITE);
	        tv.setPadding(0,0, 0, 0);/***dimension***/
	        tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	                LayoutParams.WRAP_CONTENT, (float) 0.0));
	        addView(tv);
	        tv.setClickable(false);
	        tv.setFocusable(false);
	        tv.setFocusableInTouchMode(false);
	        img.setClickable(false);
	        img.setFocusable(false);
	        img.setFocusableInTouchMode(false);
	        if(TextUtils.equals("更多", label)) {
	        	setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_spec_selector));
	        }
	        else
	        	setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_selector));
	    }
	 
	 @Override
	 public boolean onInterceptTouchEvent(MotionEvent ev) {
		 //定义弹出时的动画
		 popuwindWindow.setAnimationStyle(R.style.popupwindow_style);
		 popuwindWindow.showAsDropDown(this, -popuwindWindow.getWidth(),
					this.getTop() + popuwindWindow.getHeight() + 5);
		return super.onInterceptTouchEvent(ev);
	}
	 
	 private void init$Views() {
			HashMap<String, Object> map = null;
			map = new HashMap<String, Object>();
			map.put("ico", R.drawable.more_help_img);
			map.put("tv", "帮助");
			datas.add(map);

			map = new HashMap<String, Object>();
			map.put("ico", R.drawable.more_netword_img);
			map.put("tv", "网络");
			datas.add(map);

			map = new HashMap<String, Object>();
			map.put("ico", R.drawable.more_update_img);
			map.put("tv", "更新");
			datas.add(map);
			
			map = new HashMap<String, Object>();
			map.put("ico", R.drawable.pic_about);
			map.put("tv", "关于");
			datas.add(map);
					
			LayoutInflater inflater = LayoutInflater.from(context);
			LinearLayout  layout = (LinearLayout) inflater.inflate(R.layout.test_gd, null);
			GridView gdv = (GridView) layout.findViewById(R.id.test_gv_id);
			//进行自适应屏幕的宽度
			int tempWidth = 60 * datas.size() + 10  + (datas.size() - 1) * 5;  
			DisplayMetrics metrics = new DisplayMetrics();
			((DashboardActivity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
			int disWidth = metrics.widthPixels;
			int width = tempWidth > disWidth ? disWidth : tempWidth;
			gdv.setLayoutParams(new LayoutParams(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			SimpleAdapter simpleAdapter = new SimpleAdapter(context, datas,
					R.layout.test_gd_item, new String[] { "ico", "tv" }, new int[] {
							R.id.ico_id, R.id.tv_id });
			gdv.setAdapter(simpleAdapter);

			popuwindWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT, true);
			ColorDrawable color = new ColorDrawable(0);
			popuwindWindow.setBackgroundDrawable(color);
			gdv.setOnItemClickListener(this);
		}
		 
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			popuwindWindow.dismiss();
		}
}
 

 

 

分享到:
评论

相关推荐

    TabDemo.rar

    android的tabhost+RadioGroup+PopupWindow

    android 自定義menu加上tabhost

    @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if ((keyCode == ...// 这里写明模拟menu的PopupWindow退出就行 return true; } return false; } }); 可惜不能上传效果图

    Android各种常用的功能测试

    TabHost的使用和测试 Notification的各种支持的操作测试 图片修改:锐化、素描、怀旧等八九种样式的修改 PopupWindow的使用和测试 SingleChoice的方式选择ListView的一个条目并修改它的背景颜色和文字颜色 ViewPager...

    source.zip

    67_TraceView性能测试与Android应用性能优化方案.avi 所在项目:tabhost Android核心基础加强内容(暂不对外公布) 复杂UI界面设计、GPS与GoogleMap、自定义View、Ubuntu Linux下使用C语言面向底层开发、通过JNI进行...

    8天快速掌握Android教程源码

    67_TraceView性能测试与Android应用性能优化方案.avi 所在项目:tabhost Android核心基础加强内容(暂不对外公布) 复杂UI界面设计、GPS与GoogleMap、自定义View、Ubuntu Linux下使用C语言面向底层开发、通过JNI进行...

    传智播客的android开发源代码

    63_PopupWindow.avi 所在项目:PopupWindow 64_ListView数据异步加载与AsyncTask.avi 所在项目:DataAsyncLoad 65_ListView数据的分批加载.avi 所在项目:datapageload 66_自定义标签页.avi 所在项目:tabhost

    Android实例代码

    TabHost; ScrollView; ListView、ListActivity; ExpandableListView; GridView、ImageSwitcher; Gallery; 2.5、对话框:AlertDialog; PopupWindow; DatePickerDialog、TimePickerDialog; ProgressDialog; 2.6、...

    疯狂Android讲义源码

     2.4.7 选项卡(TabHost)的功能和  用法 93  2.4.8 滚动视图(ScrollView)的  功能和用法 95  2.4.9 列表视图(ListView和  ListActivity) 95  2.4.10 可展开的列表组件(ExpandableListView) 101  ...

    疯狂Android讲义.part2

    2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...

    疯狂Android讲义.part1

    2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...

    疯狂Android讲义(第2版)源代码 第6章~第9章

    TabHost; ScrollView; ListView、ListActivity; ExpandableListView; GridView、ImageSwitcher; Gallery; 2.5、对话框:AlertDialog; PopupWindow; DatePickerDialog、TimePickerDialog; ProgressDialog; 2.6...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--TabHost一个界面显示多Activity |--TextView单行跑马灯效果 |--TextView虚拟获得焦点 |--uploadServlet |--uri之表示资源resource |--ViewPage的使用 |--view中的tag用法之存储对象 |--view常用属性 |--xml常用...

    android开发资料大全

    android用户界面之TabHost教程实例汇总 android用户界面之Gallery教程实例汇总 android用户界面之按钮(Button)教程实例汇 android用户界面之ProgressBar教程实例汇总 android用户界面之WebView教程实例汇总 android...

Global site tag (gtag.js) - Google Analytics