如何在一个活动中显示不同的布局?

时间:2013-05-02 09:45:49

标签: android android-layout android-asynctask

我有一项活动,我需要更改布局。

在第一个布局中,我有四个要显示的按钮,在第二个布局中,我需要一个GridView来显示图像。

我需要在AsyncTask onPostExecute方法中显示第二个布局。

目前,我正在尝试设置两个setContentViews,但是我得到以下异常:ClassCastException

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_focusarea);
                                videoBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            new LoadFiles().execute();
        }
    });
    animateBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            new LoadFiles().execute();          
        }
    });
    pdfBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            new LoadPDFFiles().execute();           
        }
    });
}

在我的postExecute中,我试试这个

  protected void onPostExecute(String file_url) {
      pDialog.dismiss();    
      runOnUiThread(new Runnable() {
    public void run() {
        setContentView(R.layout.gallery); 
            girGridView=(GridView) findViewById(R.id.gridView1_bir);
            girGridView.setAdapter(new ImageAdapter(this));     
                girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
             public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
    Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();   
    }
});
                }
    });

6 个答案:

答案 0 :(得分:3)

不使用两个布局,而是使用单个布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/MyLayoutOne"
    android:layout_width="fill_parent"
    android:visibility="gone"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Hi This is my first layout" />


     <!-- Your first layout contents add here-->



</LinearLayout>

<LinearLayout
    android:id="@+id/MyLayoutTwo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Hi This is my Second layout" />


    <!-- Your second layout contents add here -->


</LinearLayout>

</LinearLayout>

在MyLayoutOne中添加第一个布局内容,在MyLayoutTwo中添加第二个布局内容

在您的活动中使用以下代码,

public class MainActivity extends Activity {


LinearLayout MyLayoutOne;
LinearLayout MyLayoutTwo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyLayoutOne=(LinearLayout)findViewById(R.id.MyLayoutOne);
    MyLayoutTwo=(LinearLayout)findViewById(R.id.MyLayoutTwo);


    // this will make first layout visible
    MyLayoutOne.setVisibility(View.VISIBLE);


    // this will make second layout hidden from your layout
    MyLayoutTwo.setVisibility(View.GONE);



    //=========================================

    //in your post create add this codes 

    //=========================================

    // this will make first layout hidden
    MyLayoutOne.setVisibility(View.GONE);

    // this will make second layout visible in your layout
    MyLayoutTwo.setVisibility(View.VISIBLE);

    //=========================================


}



}

这是一种最简单的方法,您必须研究片段以实现更好的UI管理。您也可以使用viewflipper。

所以研究碎片和Viewflipper ..

答案 1 :(得分:2)

除此之外,您可以拥有一个布局,其中包含四个按钮的包装器和GridView的其他按钮,而最后一个包含可见性设置为“已消失”。

当AsycTask完成后,您隐藏按钮布局并显示GridView布局。

答案 2 :(得分:1)

为什么不设置一个包含两个布局或两个片段的内容视图?布局可以像这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/text1">          
    </Button>
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/text2">          
    </Button>
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/text3">          
    </Button>
    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/text4">          
    </Button>
    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         ...
    </GridView>
</LinearLayout>

答案 3 :(得分:1)

Use one setContentView() and define separates Linear/Relative layout one for buttons and second for gridView.And hide/show the Views according to your need.

答案 4 :(得分:0)

AsyncTask的onPostExecute在UI线程上运行,因此您无需明确指定runonUiThread。而不是使用2 setcontent View最好在布局文件中包含2个视图,并使其在需要时显示为不可见。

答案 5 :(得分:0)

朋友将wrapper1的id更改为child,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Focusarea" >

<LinearLayout
    android:id="@+id/wrapper1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:visibility="gone" >

    <GridView
        android:id="@+id/gridView1_bir"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" >
    </GridView>
</LinearLayout>

<RelativeLayout
    android:id="@+id/wrapper2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/vid_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
       android:src="@drawable/ic_launcher" />


</RelativeLayout>

</LinearLayout>

并在oncreate之外初始化线性布局,如下所示,

LinearLayout wrapper1;
RelativeLayout wrapper2;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    wrapper1 = (LinearLayout)findViewById(R.id.wrapper1);
    wrapper2=(RelativeLayout)findViewById(R.id.wrapper2);


    // this will make first layout visible
    wrapper2.setVisibility(View.VISIBLE);


    // this will make second layout hidden from your layout
    wrapper1.setVisibility(View.GONE);


    ImageView videoBtn = (ImageView) findViewById(R.id.vid_btn);
    ImageView animateBtn = (ImageView) findViewById(R.id.anit_btn);
    ImageView pdfBtn = (ImageView) findViewById(R.id.pdf_btn);

    videoBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                new LoadFiles().execute();                         
            }
    });
    animateBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                    new LoadFiles().execute();                     
            }
    });
    pdfBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                    new LoadPDFFiles().execute();                  
            }
    });
    }
    protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting VIDEOS
            pDialog.dismiss();     
            vid=new ArrayList<String>(new ArrayList<String>(vid));
            videoUrl=parsing.parse(videoUrl);
            System.out.println("VIDEO URL" +videoUrl);
                    runOnUiThread(new Runnable() {
                            public void run() {


                            //--here you wont need to initialize again--

                                // this will make first layout visible
                                wrapper1.setVisibility(View.VISIBLE);
                                // this will make second layout hidden from your layout
                                wrapper2.setVisibility(View.GONE);

                            girGridView=(GridView) findViewById(R.id.gridView1_bir);
                            //ListView gibi buna da adapter set ediliyor.
                            girGridView.setAdapter(new ImageAdapter(this));                            
                            girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
    Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();                              
                                            }
                                    });
                            }
        });