setContentView

public void setContentView(int layoutResID)

Since: API Level 1

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

Parameters
layoutResID Resource ID to be inflated.

public void setContentView(View view)

Since: API Level 1

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invokesetContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.

Parameters
view The desired content to display

public void setContentView(View view,ViewGroup.LayoutParams params)

Since: API Level 1

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy.

Parameters
view The desired content to display.
params Layout parameters for the view.



android 通过setContentView切换Activity的View,保存动态修改后的视图

使用setContentView可以在Activity中动态切换显示的View,这样,不需要多个Activity就可以显示不同的界面,因此不再需要在Activity间传送数据,变量可以直接引用。但是,在android SDK给我们建的默认的Hello World程序中,调用的是setContentView(int layoutResID)方法,如果使用该方法切换view,在切换后再切换回,无法显示切换前修改后的样子,也就是说,相当于重新显示一个view,并非是把原来的view隐藏后再显示。其实setContentView是个多态方法,我们可以先用LayoutInflater把布局xml文件引入成View对象,再通过setContentView(View view)方法来切换视图。因为所有对View的修改都保存在View对象里,所以,当切换回原来的view时,就可以直接显示原来修改后的样子。


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="main.xml"
    />

你可能感兴趣的:(android)