代码之家  ›  专栏  ›  技术社区  ›  Christian

多次调用setContentView()

  •  12
  • Christian  · 技术社区  · 14 年前

    有办法打电话吗 setContentView(id) id 在一个活动中呈现不同的视图,还是我必须开始一个新的活动?

    3 回复  |  直到 6 年前
        1
  •  10
  •   Community CDub    8 年前

    根据奥斯汀的评论,我在另一篇文章中找到了一些关于如何使用ViewFlipper来完成这一任务的指导(参见勾选的顶部答案 here 。)

    here :

    XML格式:

    <FrameLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView 
            android:src="@drawable/icon"
            android:scaleType="fitCenter"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"/>
        <TextView
            android:text="Learn-Android.com"
            android:textSize="24sp"
            android:textColor="#000000"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:gravity="center"/>
    </FrameLayout>
    

    private void SwitchLayout2() {
    RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
    RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
    
    // Enable Layout 2 and Disable Layout 1
    Layout1 .setVisibility(View.GONE);
    Layout2.setVisibility(View.VISIBLE);
    }
    
    private void SwitchLayout1() {
    RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
    RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
    
    // Enable Layout 1 & Disable Layout2
    Layout1.setVisibility(View.VISIBLE);
    Layout2.setVisibility(View.GONE);
    }
    
        2
  •  3
  •   EboMike    14 年前

    不,你不能简单地多次调用它。您要么需要完全删除所有视图,然后对新布局进行充气,要么使用 ViewFlipper (或 FrameLayout

    另一方面,这个问题以前也有人问过,虽然我不能马上找到。

        3
  •  0
  •   Vlad    9 年前

    您可以在活动中尝试:

    getWindow().addContentView(View, ViewGroup.LayoutParams);
    

    两个内容视图将一个接一个。但是,没有直接的方法可以删除这样添加的某个视图。

    还要注意,打电话 setContentView

    推荐文章