在单个活动中使用多个布局 [英] Using multiple layouts in single activity
问题描述
我目前正在处理Android项目,该项目需要在同一活动中使用多个布局.
I am currently working on Android project where I need to use multiple layouts in the same activity.
当用户单击主布局中的按钮时,我需要具有不同内容的另一个布局(完全不同).
我尝试在Internet上查找,发现可以使用Fragments,但是据我了解,当仅需要对新布局进行部分更改而我需要使用完全不同的布局时,就可以使用Fragments.
I tried looking up on the Internet and found that Fragments can be used, but as far as I understand Fragments are to be used when one needs only partial changes in the new layout, whereas I need to use a a completely different layout.
此外,我发现包含,但这是在多个活动中使用相同的布局.所以,不是我想要的.
Also, I found include, but that is to use the same layout in multiple activities. So, not what I was looking for.
有人对如何做到这一点有任何想法吗?
Does anyone have any idea as to how to accomplish this?
推荐答案
XML使用框架布局.将您的内容放在我提到的<-您的布局->
XML use framelayout. Put your content where I have mentioned <--Your layout-->
<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并设置可见性.
Code : Set onclick for button and set visibility.
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);
}
}
这篇关于在单个活动中使用多个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!