【问题标题】:How to show different layouts in one activity?如何在一个活动中显示不同的布局?
【发布时间】:2013-05-02 09:45:49
【问题描述】:

我有一个需要更改布局的活动。

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

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

目前,我正在尝试设置两个 setContentView,但出现以下异常: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();   
    }
});
                }
    });

【问题讨论】:

  • 请发布您的 logcat 错误。

标签: android android-layout android-asynctask


【解决方案1】:

不要使用两个布局,而是使用一个布局,如下所示

<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);

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


}



}

这是一种最简单的方法,您必须研究 Fragment 才能更好地进行 UI 管理。您也可以使用 viewflipper。

所以研究 Fragments 和 Viewflipper..

【讨论】:

  • 将主LinearLayout的id更改为子LinearLayout..并在onCreate之外初始化布局...我已经在下面发布了代码..
【解决方案2】:

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

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

【讨论】:

  • @OneManArmy 很简单。在您的xml中,您在所有按钮周围放置了一个包装器布局(如linearlayout,relativelayout),并在您的gridview周围放置了另一个包装器布局)-最后一个具有可见性:消失了。此外,您需要两个包装器布局的 findviewbyid ,当异步任务完成时,您放置 wrapper1.setvisibility(view.gone) 和 wrapper2.setvisibility(view.visible)
  • plz你能贴一些xml的代码吗..请举个例子..帮助我
  • @dwbrito 请与我们分享教程!!
【解决方案3】:

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

<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>

【讨论】:

  • 如何设置两个布局??我不知道怎么做..我是android新手..请帮我设置..
  • 父布局只能有一个,但它可以包含任意数量的子布局。
【解决方案4】:
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.

【讨论】:

  • 对从 xml 布局文件中找到的视图使用 setVisbility(View.GONE) 和 setVisibility(View.VISIBLE)。a
【解决方案5】:

AsyncTask 的 onPostExecute 在 UI 线程上运行,因此您无需显式指定 runonUiThread。与其使用 2 个 setcontent View,不如在布局文件中有 2 个视图并根据需要使其可见不可见。

【讨论】:

    【解决方案6】:

    朋友把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();                              
                                                }
                                        });
                                }
            });
    

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 1970-01-01
      • 2017-05-06
      • 2016-10-02
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多