TabHost图标动态指定

最近心态不好,一个问题困扰了我这么久。

原以为要自己封闭事件,其实API就已经封装好了:


参考

http://blog.csdn.net/oppo4545/article/details/7690316


http://stackoverflow.com/questions/36881/updating-android-tab-icons


http://www.linuxidc.com/Linux/2011-12/50576.htm


http://stackoverflow.com/questions/3509178/getting-a-photo-from-a-contact  Contact ImageView


http://yelinsen.iteye.com/blog/897561 联系人性能优化

这两个组合可以重复使用布局,动态指定图标,代码写好就上上来。供大家参考



其实不用TabActivity与TabHost就可以实现,并且简单很多,ex:


	private Button button1; 
	private Button button2;
	private LinearLayout container;
	private OnClickListener l = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
			case R.id.button1:
				System.out.println("0000");
				switchActivity(0);
				break;
			case R.id.button2:
				System.out.println("1111");
				switchActivity(1);
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button1 = (Button)findViewById(R.id.button1);
		button2 = (Button)findViewById(R.id.button2);
		container = (LinearLayout) findViewById(R.id.container);
		button1.setOnClickListener(l);
		button2.setOnClickListener(l);
		switchActivity(0);
	}
	private void switchActivity(int id){
		container.removeAllViews();
		Intent intent = null;
		switch(id){
		case 0:
			intent = new Intent(this,TestActivity1.class);
			break;
		case 1:
			intent = new Intent(this,TestActivity2.class);
			break;
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		
		Window  subActivity = getLocalActivityManager().startActivity("subActivity", intent);
		container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="wrap_content">
		<Button android:id="@+id/button1" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="窗体1" />
		<Button android:id="@+id/button2" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="窗体2" />
	</LinearLayout>
	<LinearLayout android:id="@+id/container" android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		android:background="#0000ff">
	</LinearLayout>
</LinearLayout>

源代码在资源里下载。MyTabHost.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值