Android 如何将片段声明为隐藏在XML布局中
我的活动在单个XML布局中声明其所有GUI片段。它只需要在发射时显示一些碎片;其余部分在用户与应用程序交互时显示。布局的一部分如下所示:Android 如何将片段声明为隐藏在XML布局中,android,xml,fragment,Android,Xml,Fragment,我的活动在单个XML布局中声明其所有GUI片段。它只需要在发射时显示一些碎片;其余部分在用户与应用程序交互时显示。布局的一部分如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map_panel"
android:name="com.example.MapPanel"
android:layout_width="match_parent"
android:layout_height="@dimen/map_panel_height" />
<fragment
android:id="@+id/list_panel"
android:name="com.example.ListPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/map_panel" />
<fragment
android:id="@+id/detail_panel"
android:name="com.example.DetailPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/map_panel"
android:visibility="gone" />
我的意图是list_面板
片段在启动时可见,而detail_面板
片段在用户从列表中选择某个内容之前是隐藏的
默认情况下,片段以ishiden
属性false开头。这意味着我的活动必须迭代加载的片段,并在启动时手动调用细节面板等片段上的ishiden(true)
我更愿意在XML布局中声明ishiden
状态。但是,在
声明中设置android:visibility=“gone”
并不会改变ishiden
状态,我也找不到任何关于另一个属性的文档来实现这一点
是否可以在
上设置XML属性以使其隐藏
注意:我不关心视图可见性,我关心的是fragment.ishiden()
值。这会影响FragmentManager如何操纵后堆栈并执行动画。如果对视图不可见或已消失的片段调用transaction.show(fragment)
,但fragment.ishiden()
值为false,则FragmentManager将不会使视图可见。请参阅以供参考。我们有一个用于片段的isVisible方法
看到逝去的人不会占用任何空间
“隐形”占据了实际的视野空间。这个答案有点晚了,认为它可能有助于将来的参考。可见性是视图类的一部分-Fragment扩展对象,但无法访问可见性值。一种可能性是使片段成为FrameLayout的子级,并在布局上调用invisible或gone。这将导致片段看起来被隐藏
希望有帮助 我面临着类似的情况,我不得不隐藏一个片段
public void showHideFrgament(final Fragment fragment){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
if (fragment.isHidden()) {
ft.show(fragment);
Log.d("hidden","Show");
} else {
ft.hide(fragment);
Log.d("Shown","Hide");
}
ft.commit();
}
我只是将片段包含在线性布局中,并将布局标记为可见/消失
<LinearLayout
android:id="@+id/map_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
根据Jyo的帖子,使用以下内容:
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(mFragment);
fragmentTransaction.commit();
这在API级别23上对我有效MFFragment
是您要隐藏的片段。您是否尝试过android:visibility=“invisible”?是的,除了“gone”之外,我还尝试过“invisible”。两者都不会影响碎片的隐藏状态。你所说的“除了消失”是什么意思。你刚才试过“隐形”吗。在这种情况下,它将创建碎片并使其处于不可见状态。不,在标记中设置android:visibility=“invisible”不会影响碎片的isHidden状态。@JayLieske找到答案了吗?您正在描述视图的状态,不是片段的isHidden
状态。这与谷歌建议的使用FragmentManager隐藏/显示片段的做法相反。isVisible无法更改,因为它在Kotlin中是val更改视图的visibility
状态对片段的isHidden
状态没有影响。包装fragment
围绕一个FrameLayout
是最简单和最有效的图片。这是针对谷歌的建议,即使用FragmentManager隐藏/显示片段。这是正确的答案。不应使用视图可见性隐藏片段。