android编程实现局部界面动态切换的方法

时间:2021-12-18 06:50:19

本文实例讲述了android编程实现局部界面动态切换的方法。分享给大家供大家参考,具体如下:

局部界面固定,局部界面可以动态切换。效果如下:

android编程实现局部界面动态切换的方法

android编程实现局部界面动态切换的方法

android编程实现局部界面动态切换的方法

这个效果由3个layout构成

main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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

?
1
2
3
4
5
6
7
8
9
10
11
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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程序设计有所帮助。