开始使用了一个Activity做界面切换,采用visible、gone写法,感觉太麻烦了。
layoutHousehold.setVisibility(View.GONE);
layoutCamera.setVisibility(View.GONE);
layoutEnvironment.setVisibility(View.GONE);
layoutMsg.setVisibility(View.GONE);
layoutSetting.setVisibility(View.GONE);
layoutLight.setVisibility(View.VISIBLE);
一般使用MainActivity集成Activity类的时候,通过setContentView(Layout)设计当前的图层,线程称为主线程,当你在定一个类的时候你是不能更改当前的UI的,也就是说一个Activity UI界面对应一个类,不能使用另外的类进行更改,毕竟更新UI需要主线程来完成,新的类不再主线程中(个人观点)。
那么怎么通过导航栏做Activity切换?使用ViewGroup中的TabHost。
效果:
1、这里我使用了Tabhost:
这里导航栏使用setIndicator不是特别灵活,还有就是自己对setIndicator(View)中的view理解的不对,经常出现让我移除图层的错误,所以采用单击监听事件:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
tabhost = (TabHost) this.findViewById(android.R.id.tabhost);//获取当前的tabhost,注册 tabhost.setup();
tabhost.addTab(tabhost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(new Intent(this, light.class)));
tabhost.addTab(tabhost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(new Intent(this, Household.class)));
tabhost.addTab(tabhost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(new Intent(this, Camera.class)));
tabhost.addTab(tabhost.newTabSpec("tab4")
.setIndicator("tab4")
.setContent(new Intent(this, Environment.class)));
tabhost.addTab(tabhost.newTabSpec("tab5")
.setIndicator("tab5")
.setContent(new Intent(this, Msg.class)));
tabhost.addTab(tabhost.newTabSpec("tab6")
.setIndicator("tab6")
.setContent(new Intent(this, Setting.class)));
tabhost.setCurrentTabByTag("tab1");
ctrlCmd(); }
2、事件监听与图片切换
/***************************************
* 设定面板邮编图标,单击当前图标会变成绿色,同时加载当前按钮对应的activity
*/
public void ctrlCmd(){
ctrlLight=(ImageView)this.findViewById(R.id.ctrl_light);
ctrlHousehold=(ImageView)this.findViewById(R.id.ctrl_household);
ctrlCamera=(ImageView)this.findViewById(R.id.ctrl_camera);
ctrlEnvironment=(ImageView)this.findViewById(R.id.ctrl_environment);
ctrlMsg=(ImageView)this.findViewById(R.id.ctrl_msg);
ctrlSetting=(ImageView)this.findViewById(R.id.ctrl_setting); ctrlonclick ctrl = new ctrlonclick();
ctrlLight.setOnClickListener(ctrl);
ctrlHousehold.setOnClickListener(ctrl);
ctrlCamera.setOnClickListener(ctrl);
ctrlEnvironment.setOnClickListener(ctrl);
ctrlMsg.setOnClickListener(ctrl);
ctrlSetting.setOnClickListener(ctrl);
}
/************************************************************
* 做一个类用来监听按键的触发程序
* @author hanzhen
*
*/
class ctrlonclick implements OnClickListener{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0 == ctrlLight){
Log.i(TAG, "------>>> ctrlLight is pressed");
ctrlLight.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_light2));
ctrlHousehold.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_household1));
ctrlCamera.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_camera1));
ctrlEnvironment.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_environment1));
ctrlMsg.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_msg1));
ctrlSetting.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_set1));
tabhost.setCurrentTabByTag("tab1");
}else if(arg0 == ctrlHousehold){
Log.i(TAG, "------>>> ctrlHousehold is pressed");
ctrlLight.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_light1));
ctrlHousehold.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_household2));
ctrlCamera.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_camera1));
ctrlEnvironment.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_environment1));
ctrlMsg.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_msg1));
ctrlSetting.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_set1));
tabhost.setCurrentTabByTag("tab2");
}else if(arg0 == ctrlCamera){
Log.i(TAG, "------>>> ctrlCamera is pressed");
ctrlLight.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_light1));
ctrlHousehold.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_household1));
ctrlCamera.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_camera2));
ctrlEnvironment.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_environment1));
ctrlMsg.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_msg1));
ctrlSetting.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_set1));
tabhost.setCurrentTabByTag("tab3");
}else if(arg0 == ctrlEnvironment){
Log.i(TAG, "------>>> ctrlEnvironment is pressed");
ctrlLight.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_light1));
ctrlHousehold.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_household1));
ctrlCamera.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_camera1));
ctrlEnvironment.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_environment2));
ctrlMsg.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_msg1));
ctrlSetting.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_set1));
tabhost.setCurrentTabByTag("tab4");
}else if(arg0 == ctrlMsg){
Log.i(TAG, "------>>> ctrlMsg is pressed");
ctrlLight.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_light1));
ctrlHousehold.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_household1));
ctrlCamera.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_camera1));
ctrlEnvironment.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_environment1));
ctrlMsg.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_msg2));
ctrlSetting.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_set1));
tabhost.setCurrentTabByTag("tab5");
}else if(arg0 == ctrlSetting){
Log.i(TAG, "------>>> ctrlSetting is pressed");
ctrlLight.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_light1));
ctrlHousehold.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_household1));
ctrlCamera.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_camera1));
ctrlEnvironment.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_environment1));
ctrlMsg.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_msg1));
ctrlSetting.setImageDrawable(getResources().getDrawable(R.drawable.rbtn_set2));
tabhost.setCurrentTabByTag("tab6"); }
}
}
遇上的问题:
当我做好xml的布局文件时,我使用
setContentView(R.layout.main1);
出现了错误:
07-05 08:59:02.539: W/dalvikvm(3502): threadid=1: thread exiting with uncaught exception (group=0x40a521f8)
07-05 08:59:02.564: E/AndroidRuntime(3502): FATAL EXCEPTION: main
07-05 08:59:02.564: E/AndroidRuntime(3502): java.lang.NullPointerException
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.widget.TabWidget.dispatchDraw(TabWidget.java:340)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.View.draw(View.java:10981)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.View.draw(View.java:10981)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
07-05 08:59:02.564: E/AndroidRuntime(3502): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
主要是在UI中存在TabHost必须要做处理。
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TabHost tabhost = (TabHost) this.findViewById(android.R.id.tabhost);//获取当前的tabhost,注册 tabhost.setup();
tabhost.addTab(tabhost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(new Intent(this, light.class)));