【发布时间】:2015-08-17 10:02:53
【问题描述】:
我有一个带有 NavigationDrawer 和工具栏的 Activity。当我想要在滚动一个子片段视图时消失一个工具栏时,我遇到了一个问题。一切正常,除了从底部出现的一些视图与正在消失的工具栏的大小完全相同。
我制作了一个 GIF 动画来显示问题。由于我的声誉,我无法直接发布图片,但 this gif 和 this gif 显示了问题
我试图弄清楚这是从哪里来的。似乎它来自我的容器 FrameLayout,我的片段视图在运行时放置在其中。我将其背景更改为绿色,以便我可以识别它。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkGreen"
tools:context="org.cddevlib.breathe.MainActivity" />
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="org.cddevlib.breathe.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.DrawerLayout>
这是包含的工具栏布局
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
最后是加载的片段视图中的 sn-p:
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
layout="@layout/toolbar" />
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/white"
android:fillViewport="true" >
(...)
请注意,我在该布局中再次包含工具栏,因为我希望通过隐藏/添加工具栏为我的应用程序中的每个片段使用不同的工具栏。
toolbar = (Toolbar) ((View) vw).findViewById(R.id.toolbar);
if (toolbar != null) {
// // for crate home button
activity = (AppCompatActivity) getActivity();
activity.getSupportActionBar().hide();
toolbar.setBackgroundDrawable(new ColorDrawable((ColorUtils.getColorDark(DataModule.getInstance()
.getMainActivity()))));
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
解决方案
因此,感谢 sanat shukla 的回答,我的问题是我在片段视图中使用了 AppBarLayout 太多。在我的片段视图中,AppBarLayout 是我所有组件的主要布局,但它不应该!它旨在保存工具栏内容!谢谢
【问题讨论】:
标签: android android-fragments material-design