2017-06-04 50 views
0

我想制作类似这样的照片(照片)。当一个按钮被按下时(在这个应用程序中显示“suchen”的红色按钮)显示主活动屏幕一部分的一个片段。每当我按下其他按钮时,相应的活动就会显示在同一活动中。如何在按下相应按钮时在活动中部分显示多个片段

enter image description hereenter image description here

当按下每个按钮 可以看到的相应片段此按钮在屏幕,其中用于片段布局的部分。我这样做是这样的:

<?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代码中可见。所以我在主活动中有四个布局容器,它们是隐藏的,只有在按下相应的按钮时才显示。这是正确的方式吗?有没有正确的方法? 先进的谢谢!

+0

从图片中,可能是'ViewPager'带有两个'fragments'。尝试通过自定义'DialogFragment'来实现。 – Yupi

+0

@Yupi我只是想要我显示的片段是一个带有后退按钮的普通视图,所以我可以返回到主屏幕。我正在做的是工作。问题是:是对的?这是做这种事情的正确方法吗? – user4938227

回答

0

这是您为实现目标所做的一种方式,但这不是好的方法。您不必制作四个片段,而是可以有一个片段,并在您按另一个按钮时将其替换。如果您想更换片段

// Create fragment and give it an argument specifying the article it should show 
ArticleFragment newFragment = new ArticleFragment(); 
Bundle args = new Bundle(); 
args.putInt(ArticleFragment.ARG_POSITION, position); 
newFragment.setArguments(args); 

FragmentTransaction transaction = 
getSupportFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack so the user can navigate back 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 

注意:当您删除或替换片段和交易添加到回堆,被移除的停止片段(不破坏)。如果用户返回来恢复片段,则重新启动。如果您不将事务添加到后退堆栈,那么在移除或替换时碎片将被销毁。要允许用户向后浏览片段事务,必须在提交FragmentTransaction之前调用addToBackStack()

+0

非常感谢您的回答。因此,我在照片中展示的应用程序正在使用您展示的方式?编码的“问题”在于有一百种可能的方式来做某件事,但并非所有的方法都是正确的!再次感谢。我会尝试这种方式☺ – user4938227

+0

欢迎您。只有制作应用程序是不够的,您编写应用程序的效率有多重要。所以,祝你好运。 –

相关问题