【问题标题】:Unable to hide a Fragment with Fragment Manager无法使用片段管理器隐藏片段
【发布时间】:2014-01-07 03:56:46
【问题描述】:

我试图在我的活动开始时隐藏一个片段,我的布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="#eee"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<fragment android:name="com.TypeFragment"
          android:id="@+id/type_fragment"
          android:layout_width="184dp"
          android:layout_alignParentLeft="true"
          android:layout_height="wrap_content" 
          android:text="@string/type"
          />

<fragment android:name="com.ModeFragment"
          android:id="@+id/mode_fragment"
          android:layout_below="@id/type_fragment"
          android:layout_width="184dp"
          android:layout_alignParentLeft="true"
          android:layout_height="wrap_content" 
          android:text="@string/mode"
          />
<fragment
    android:id="@+id/canvas_fragment"
    android:name="com.CanvasFragment"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/type_fragment" />

<fragment android:name="com.NumericsInputFragment"
    android:id="@+id/numeric_area"
    android:layout_below="@id/mode_fragment"
    android:layout_width="184dp"
    android:layout_height="wrap_content" />

</RelativeLayout>

我的 MainActivity 扩展了 FragmentActivity 并具有以下 onCreate

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_canvas);

    Log.d("ACTIVITY" ,  "linside");
    // Check which layout we are using
    if(findViewById(R.id.fragment_container) != null) {
        /* if it is restored from a previous state - simply return */
         if (savedInstanceState != null) {
                return;
            }
         /* Create a TypeFragment */
         TypeFragment firstFragment = new TypeFragment();
         //firstFragment.getView().setBackgroundColor(Color.GRAY);
         ModeFragment secondFragment = new ModeFragment();
         /* This is the numerics Fragement */
         NumericsInputFragment numericsFragment = new NumericsInputFragment();

         /* Pass the Intents Extras to the Fragment */
         firstFragment.setArguments(getIntent().getExtras());
         secondFragment.setArguments(getIntent().getExtras());

         /* Add the fragment to the fragment container */
         getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, firstFragment, null)
            .add(R.id.fragment_container, secondFragment, null)
            .add(R.id.fragment_container, numericsFragment, null)
            .commit();

         FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.hide(getFragmentManager().findFragmentById(R.id.numeric_area));
            transaction.commit();
    }
}

我所做的一切似乎都不会改变我的初始布局。即,如果我删除所有 .add() 语句,我仍然会得到相同的布局。显然我在这里做错了什么,有人可以解释一下我可能做错了什么吗?

编辑

我已将布局替换为更新版本,将动态片段替换为 FrameLayout,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment android:name="com.TypeFragment"
          android:id="@+id/type_fragment"
          android:layout_width="184dp"
          android:layout_alignParentLeft="true"
          android:layout_height="wrap_content" 
          android:text="@string/type"
          />

<fragment android:name="com.ModeFragment"
          android:id="@+id/mode_fragment"
          android:layout_below="@id/type_fragment"
          android:layout_width="184dp"
          android:layout_alignParentLeft="true"
          android:layout_height="wrap_content" 
          android:text="@string/mode"
          />
<fragment
    android:id="@+id/canvas_fragment"
    android:name="com.CanvasFragment"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/type_fragment" />

<FrameLayout 
    android:id="@+id/numeric_area"
    android:layout_width="184dp"
    android:layout_height="match_parent"
    android:layout_below="@id/mode_fragment"
    android:background="?android:attr/detailsElementBackground"
/>
</RelativeLayout>

然后我可以控制是否按照此处http://developer.android.com/guide/components/fragments.html 的描述显示

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    您不能添加/替换/隐藏您在布局文件中静态定义的片段。您可以使用 Framelayout 并使用 FragmentManager/FragmentTransaction 将其替换为片段实例。 Check out this link

    【讨论】:

    • 好的,谢谢...如果我使用的是相对布局,是否可以以正确的方式以编程方式将片段添加到相对布局?
    • RelativeLayout 仍然可以是父级。您可以将 FrameLayout(s) 作为其子项。这不是确切的代码。这只是您可以构建的示例。 schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="16dp" android:paddingRight="16dp" >
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多