android编程实现局部界面动态切换的方法
程序员文章站
2023-12-04 09:10:16
本文实例讲述了android编程实现局部界面动态切换的方法。分享给大家供大家参考,具体如下:
局部界面固定,局部界面可以动态切换。效果如下:
这个效果由3个...
本文实例讲述了android编程实现局部界面动态切换的方法。分享给大家供大家参考,具体如下:
局部界面固定,局部界面可以动态切换。效果如下:
这个效果由3个layout构成
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="@android:color/black" > <button android:id="@+id/btnswitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="switch" /> <button android:id="@+id/btnscreen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="screen" /> </linearlayout> <linearlayout android:id="@+id/frameswitch" android:layout_width="160dp" android:layout_height="fill_parent" android:background="@android:color/white" > </linearlayout> </linearlayout>
one.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/yellow" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is linearlayout one" /> </linearlayout>
two.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is linearlayout two" /> <button android:id="@+id/btnsecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btnsecond" /> </linearlayout>
下面是java代码
public class zzzandroidactivity extends activity { private linearlayout frameswitch; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); frameswitch = (linearlayout) findviewbyid(r.id.frameswitch); button btnswitch = (button) findviewbyid(r.id.btnswitch); btnswitch.setonclicklistener(new onclicklistener() { boolean boo = false; @override public void onclick(view v) { boo = !boo; if (boo) { getviewone(); } else { getviewsecond(); } } }); /* * 是否全屏 */ button btnscreen = (button) findviewbyid(r.id.btnscreen); btnscreen.setonclicklistener(new onclicklistener() { boolean isscreen = false; @override public void onclick(view v) { isscreen = !isscreen; if (isscreen) { frameswitch.setvisibility(android.view.view.gone); } else { frameswitch.setvisibility(android.view.view.visible); } } }); } public void getviewone() { view viewone = getlayoutinflater().inflate(r.layout.one, null); frameswitch.removeallviews(); frameswitch.addview(viewone, layoutparams.fill_parent, layoutparams.fill_parent); } public void getviewsecond() { view viewsecond = getlayoutinflater().inflate(r.layout.two, null); button btn = (button) viewsecond.findviewbyid(r.id.btnsecond); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(zzzandroidactivity.this, "hello world", toast.length_long).show(); } }); frameswitch.removeallviews(); frameswitch.addview(viewsecond, layoutparams.fill_parent, layoutparams.fill_parent); } }
希望本文所述对大家android程序设计有所帮助。