我有一个带有导航抽屉的活动,它替换了活动上的 main_fragment_container 。
当显示其中一个片段时,我想更改工具栏的布局并向其添加一个微调器(并在片段隐藏时将其删除)。
我的布局看起来像这样:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
sothree:theme="@style/AppTheme.ActionBar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main layout -->
<FrameLayout
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Nav drawer -->
<fragment
android:id="@+id/fragment_drawer"
android:name="com.idob.mysoccer.ui.DrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left|start" />
</android.support.v4.widget.DrawerLayout>
不确定您想要完成什么,但我认为,如果可能的话,您应该通过让片段自定义工具栏而不是替换它来解决此问题。您可以根据您的需要让片段在工具栏上隐藏/显示视图。
Add setHasOptionsMenu(true);
在碎片中OnCreateView()
然后覆盖onOptionsMenuCreated()
像这样:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.result_list, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.this_frag_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
如果您需要使用工具栏执行更具体的操作,您可以使用以下命令获取实例
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)