繁体   English   中英

在单个活动中使用多个布局

[英]Using multiple layouts in single activity

我目前正在处理Android项目,该项目需要在同一活动中使用多个布局。

当用户单击主布局中的按钮时,那么我需要具有不同内容的另一个布局(完全不同)。

我尝试在Internet上查找,发现可以使用Fragments,但是据我了解,当仅需要对新布局进行部分更改而我需要使用完全不同的布局时,就可以使用Fragments。

另外,我发现包含,但这是在多个活动中使用相同的布局。 所以,不是我想要的。

有谁知道如何实现这一目标?

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

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

我不认为片段只能用于部分更改,希望它也适用于动态视图

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM