【问题标题】:Change the view in the Android Studio在 Android Studio 中更改视图
【发布时间】:2018-11-08 14:16:58
【问题描述】:

我在另一个论坛上尝试,感谢您的回答,但这太难了! :)

【问题讨论】:

  • 我相信您可以在这里获得更多帮助 如果您选择对您来说最简单的答案,然后继续提出更多问题以确保一切都清楚。此外,您到底想做什么也不是很清楚,所以您得到的答案有点笼统。除非您提供项目的所有必要代码,并准确解释您的需求,否则您不应期望复制粘贴的答案来解决您的所有问题。

标签: android button layout fragment


【解决方案1】:

您应该在活动布局中为您的片段创建特定布局,并在替换方法中指定其 id。更多信息请阅读fragment 文档

【讨论】:

    【解决方案2】:

    这里是FragmentTransaction.replace()的文档

    第一个参数是包含片段的容器视图的 ID,而不是布局的 ID

    第二个是你要显示的 Fragment 的实例。

    一般情况下是这样的:

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, myFragment);
    fragmentTransaction.commit();
    

    【讨论】:

      【解决方案3】:

      首先,您需要在主 Activity 中有一个框架布局,您可以在其中放置片段并向用户展示。可以这样做:

      <android.support.constraint.ConstraintLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context=".HomeActivity"
      tools:showIn="@layout/app_bar_home">
      
      <FrameLayout
      
          android:id="@+id/frm"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:layout_editor_absoluteX="8dp"
          tools:layout_editor_absoluteY="8dp">
      
      </FrameLayout>
      

      现在在框架布局 (id = frm) 中,您可以根据用户的按钮点击来放置各种片段。现在,默认情况下将第一个片段放在框架布局中,并在该片段中有一个按钮。这可以通过在您的主要活动中添加以下行来完成:

      fragmentTransaction.replace(R.id.frm,new FirstFragment()).addToBackStack(null).commit();
      

      单击该按钮后,您可以编写逻辑以将第一个片段替换为第二个片段。使用下面的示例代码:

      您的第一个片段:

      <?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="wrap_content"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:background="@color/litegrey">
      
          <Button
            android:id="@+id/frag2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go to fragment 2" />
      
      </LinearLayout>
      

      移到下一张幻灯片的逻辑(这应该添加到您的第一个片段中):

      frag2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  fragmentTransaction.replace(R.id.frm,new SecondFragment()).addToBackStack(null).commit();
              }
          });
      

      【讨论】:

      • 非常感谢!因此,由于我对片段非常陌生,因此我对有关此答案的一些准则有一些疑问: 1. 在主要活动中,在添加带有“无法解析符号的片段交易”的行时会发生错误。 " 2.对于我的第一个片段,你的意思是我必须为每个片段创建一个新的布局,我只是想确保我理解你的意思。 3. 你的意思是我也应该为每个片段创建一个新类吗?
      • 1.您的错误中未解决哪个符号? frmFirstFragment ? 2. 每当你创建一个新的片段时,它都会有自己的布局。您不必单独创建它。 3. 创建片段与创建活动非常相似。在您将创建活动的同一列表中查找选项fragment 并单击它。 Android Studio 将为您创建一个新的 Fragment,并为该 Fragment 提供相应的布局。
      【解决方案4】:

      您可以创建一个包含 frameLayout 的 Activity,其 id 类似于 fragment_container。

      然后从您的活动中执行以下操作:

      Fragment newFragment = new ExampleFragment();
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      transaction.replace(R.id.fragment_container, newFragment);
      transaction.addToBackStack(null);
      transaction.commit();
      

      这将在您的 Activity 布局中放置一个片段。当你想替换它时,你只需创建第二个片段并做同样的事情。如果您想让它更容易,只需将其用作方法:

      public void replaceFragment(Fragment fragment, Context context) {
          FragmentTransaction transaction = context.getFragmentManager().beginTransaction();
          transaction.replace(R.id.fragment_container, fragment);
          transaction.addToBackStack(null);
          transaction.commit();
      }
      

      当您需要显示一个片段或将一个片段交换为已经显示的片段时使用它。作为 Context 使用保存片段的活动。

      这个想法是你有一个活动作为片段的包装器。在其布局文件中,您为要显示的片段创建一个空间,并使用上述方法,将空白空间替换为片段的布局。如果您在参数中使用不同的片段再次调用它,它将自动用新片段的布局替换该空间。

      编辑:如果您想使用按钮来回切换,请在您的活动布局中添加一个按钮并设置一个 onClick 侦听器。然后使用一个标志来选择要显示的片段。

      boolean isFragmentOneDisplayed;

      button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (isFragmentOneDisplayed) {
                      FragmentTwo fragment = new FragmentTwo();
                      replaceFragment(fragment, MainActivity.this);
                      isFragmentOneDisplayed = false;
                   } else {
                       FragmentOne fragment = new FragmentOne();
                       replaceFragment(fragment, MainActivity.this);
                       isFragmentOneDisplayed = true;
                   }
              }
          });
      

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多