我想制作类似这样的照片(照片)。当一个按钮被按下时(在这个应用程序中显示“suchen”的红色按钮)显示主活动屏幕一部分的一个片段。每当我按下其他按钮时,相应的活动就会显示在同一活动中。如何在按下相应按钮时在活动中部分显示多个片段
当按下每个按钮 可以看到的相应片段此按钮在屏幕,其中用于片段布局的部分。我这样做是这样的:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
<Button
android:id="@+id/first_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/second_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/third_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/forth_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Relativelayout
android:layout_below="@id/first_btn"
android:id="@+id/first_fragment_container"
android:layout_width="300dp"
android:layout_height="250dp">
<fragment
android:id="@+id/fragment1"
android:name="com.example.ulrich.kitchentimer.fragments.FirstFragmentActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/first_fragment_activity" />
</Relativelayout>
<Relativelayout
android:layout_below="@id/first_btn"
android:id="@+id/second_fragment_container"
android:layout_width="300dp"
android:layout_height="250dp">
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragments.SecondFragmentActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/second_fragment_activity" />
</Relativelayout>
<Relativelayout
android:layout_below="@id/first_btn"
android:id="@+id/third_fragment_container"
android:layout_width="300dp"
android:layout_height="250dp">
<fragment
android:id="@+id/fragment3"
android:name="com.example.fragments.ThirdFragmentActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/third_fragment_activity" />
</Relativelayout>
<Relativelayout
android:layout_below="@id/first_btn"
android:id="@+id/forth_fragment_container"
android:layout_width="300dp"
android:layout_height="250dp">
<fragment
android:id="@+id/fragment4"
android:name="com.example.fragments.ForthFragmentActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/forth_fragment_activity" />
</Relativelayout>
</LinearLayout>
正如你可以在我的代码中看到我用四个按钮,每个按钮打开一个其他片段,但在主活动在同一个地方。所以我有四个布局来包含这四个片段中的每一个。我将每个布局设置为在XML代码中不可见,并且当每个按钮被按下以打开相应的片段时,我将布局设置为在Java代码中可见。所以我在主活动中有四个布局容器,它们是隐藏的,只有在按下相应的按钮时才显示。这是正确的方式吗?有没有正确的方法? 先进的谢谢!
从图片中,可能是'ViewPager'带有两个'fragments'。尝试通过自定义'DialogFragment'来实现。 – Yupi
@Yupi我只是想要我显示的片段是一个带有后退按钮的普通视图,所以我可以返回到主屏幕。我正在做的是工作。问题是:是对的?这是做这种事情的正确方法吗? – user4938227