繁体   English   中英

片段内容覆盖工具栏

[英]Fragment content is overlaying Toolbar

我最近开始研究 AppCompat 21 的新组件并实现 Material Design。 目前,我有一个带有工具栏的 ActionBarActivity 并试图让它托管一个包含 TextView 项目的 RecyclerView 的片段(只是为了测试 Recycler)。 我有项目正在显示,但每个视图中的文本都被切断了,整个 Recycler 都像这样覆盖了工具栏:

此处说明

如您所见,共有三个 TextView。 他们的文字在中途被切断,它覆盖在工具栏上(我不知道标题)。 TextView 项目布局包含在 RecyclerView 布局中,这是 Fragment 的布局。 父Activity有一个FrameLayout -> Toolbar, FrameLayout。 我正在将 Fragment 插入到 Activity 的子 FrameLayout 中。 这是 XML:

Recycler 中的每个视图:

<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/textview"
  android:layout_width="match_parent"
  android:layout_height="48dp"
  android:fontFamily="sans-serif"
  android:paddingTop="16dp"
  android:paddingBottom="20dp"
  android:textSize="16sp"/>

Recycler布局,也就是Fragment的布局:

<android.support.v7.widget.RecyclerView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/recycler_tasks"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="stuff.MainActivity$TaskFragment">
</android.support.v7.widget.RecyclerView>

和父 Activity 的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  tools:ignore="MergeRootFrame">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</FrameLayout>

我知道它一定很简单,但我已经被它难住了一段时间,尝试了各种方法都无济于事。

请不要忘记将app:layout_behavior="@string/appbar_scrolling_view_behavior"到您的内容布局

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

   </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

使用RelativeLayout而不是FrameLayout作为顶级父级。 然后只需为Toolbar添加layout_above或片段容器的layout_below等依赖layout_above

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM