Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使片段在初始化时不可见_Android - Fatal编程技术网

Android 使片段在初始化时不可见

Android 使片段在初始化时不可见,android,Android,我有一个活动,其中包含两个片段,现在我希望它们在创建活动时不可见,我将根据用户输入显示片段 因此,我尝试在布局文件中设置片段不可见: <fragment android:name="com.app.ui.ArticleListFragment" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/poiSearchFrag"

我有一个活动,其中包含两个
片段
,现在我希望它们在创建
活动
时不可见,我将根据用户输入显示片段

因此,我尝试在布局文件中设置
片段
不可见:

<fragment
    android:name="com.app.ui.ArticleListFragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/poiSearchFrag"
    android:visibility="gone"></fragment>

<fragment
    android:name="com.app.ui.ArticleReaderFragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/routeSearchFrag"
    android:visibility="gone"></fragment>

但这似乎不起作用


如何修复?

您可以尝试将片段放在不同的布局中,然后使用layout.setVisibility属性根据用户输入更改可见性。因此,布局将是:

Layout1
  |
  Fragment 1
Layout2
  |
  Fragment 2

“似乎不起作用”是指当活动启动时,两个片段都可见。请尝试以编程方式使用fragmenttransaction的hide()方法。您可以始终设置为片段内部的视图,而不是片段