2014-10-26 66 views
0

我目前正在开发Android项目,我需要在同一活动中使用多个布局。在单个活动中使用多个布局

当用户点击主布局中的一个按钮时,我需要另一个布局(这是完全不同的)与不同的内容。

我试着在互联网上查找,发现可以使用碎片,但据我了解,碎片将用于新布局只需要部分更改时使用碎片,而我需要使用完全不同的碎片布局。

此外,我发现包括,但是这是在多个活动中使用相同的布局。 所以,不是我在找什么。

有没有人有任何想法如何做到这一点?

+0

当他按下按钮时,为什么不用所需的布局打开一个新的活动? – TomerZ 2014-10-26 15:24:28

+0

使用视图分页器。然后按钮上点击只是改变布局..删除旧的布局,并添加新的布局。 – Meenal 2014-10-26 15:29:50

+0

你可以做setContentView(R.layout.your_new_layout); – 2014-10-26 15:30:16

回答

0

尝试使用fragments
您可以在一个活动显示,许多片段

0

我不这么认为片段只能用于局部修改使用,希望它与动态视图以及良好

http://developer.android.com/guide/components/fragments.html 
2

XML使用framelayout。把你的内容,我刚才提到< - 您的布局 - >

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



     <LinearLayout 
      android:id="@+id/Layout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

          <--Your layout--> 

        <Button 
         android:id="@+id/Button1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="10dp" 
         android:text="Button" /> 
       </LinearLayout> 



    <LinearLayout 
     android:id="@+id/Layout2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <--Your layout--> 
    </LinearLayout> 

</FrameLayout> 

代码:的onclick设置按钮,设置的知名度。

LinearLayout 1ayout1,layout2; 
Button button1; 

1ayout1 = (LinearLayout) findViewById(R.id.Layout1); 
1ayout2 = (LinearLayout) findViewById(R.id.Layout2); 
button1=(Button)findViewById(R.id.Button1); 
button1.setOnClickListener(this); 

1ayout1.setVisibility(LinearLayout.VISIBLE); 
1ayout2.setVisibility(LinearLayout.GONE); 

@Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
      1ayout2.setVisibility(LinearLayout.VISIBLE); 
      1ayout1.setVisibility(LinearLayout.GONE); 
    } 
} 
相关问题