片段android:xml布局定义中的可见性

片段android:xml布局定义中的可见性,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,它是如何工作的?我的布局如下: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:id="@+id/search_form_fragment" android:name="FragmentC

它是如何工作的?我的布局如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/search_form_fragment"
        android:name="FragmentClass"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/result_list_fragment"
        android:name="FragmentClass"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
</LinearLayout>
返回我不期望的
true
。我想知道使用android:visibility是否正确,因为我在文档中找不到关于它的任何信息。

我尝试过这样做

XML

我的日志是:

05-09 19:07:54.236: D/visibility(3483): false
在android中,如果片段当前对用户可见,则isVisible()返回true。这意味着它:(1)已添加,(2)其视图已附加到窗口,并且(3)未隐藏

也许你还没有添加片段?从代码上我看不出来。希望这能有所帮助。

根据,
isVisible
的定义如下:

 final public boolean isVisible() {
    return isAdded() && !isHidden() && mView != null
            && mView.getWindowToken() != null && 
               mView.getVisibility() == View.VISIBLE;
}
即,它连接到活动,不隐藏(通过),视图膨胀,视图连接到窗口,片段的内部视图为
视图。可见

我认为问题在于,为了使碎片膨胀,系统会创建一个布局来保存碎片的视图。正是该视图被设置为
view.GONE
,而不是片段创建的内部视图

我可能建议将您的病情改为:

findViewById(R.id.result_list_fragment).getVisibility() == View.VISIBLE

您是否正确初始化(获取对)
firstfragment
secondfragment
的引用?是的,我通过
getFragmentById()
获取引用。如
firstfragment=getFragmentManager().findffragmentbyid(R.id.search\u form\u fragment)
secondFrag=setFragmentManager().findFragmentById(R.id.result\u list\u fragment)?是的,这就是我获取片段引用的方式。好吧,那我就输了。我只是想让你确保代码中没有什么愚蠢的错误。:)好吧,祝你好运。好吧,但你可以通过编程添加片段。我想知道android:visibility是如何在xml标记上工作的。然后我认为ianhanniballake就是你们想要的答案。
05-09 19:07:54.236: D/visibility(3483): false
 final public boolean isVisible() {
    return isAdded() && !isHidden() && mView != null
            && mView.getWindowToken() != null && 
               mView.getVisibility() == View.VISIBLE;
}
findViewById(R.id.result_list_fragment).getVisibility() == View.VISIBLE