片段内容重叠在工具栏上

7
我最近开始尝试使用AppCompat 21的新组件并实现Material Design。目前,我有一个ActionBarActivity和一个Toolbar,并试图让它托管一个包含一些文本视图项目(仅为了测试Recycler)的片段。我已经显示了这些项,但每个视图中的文本被截断了,整个Recycler重叠在工具栏上,如下图所示:
https://istack.dev59.com/ARndR.webp 您可以看到,有三个TextViews。它们的文本被截断了一半,覆盖在工具栏上(没有标题)。TextView项目布局包含在RecyclerView布局中,后者是片段的布局。父活动有一个FrameLayout -> Toolbar,FrameLayout。我将片段插入到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"/>

回收站布局,是片段的布局方式:
<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>

我知道这一定是很简单的问题,但我已经被困扰了一段时间,尝试了各种方法都没有成功。

2个回答

22
请不要忘记在您的内容布局中添加 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>

9

使用 RelativeLayout 替代 FrameLayout 作为顶级父容器。然后只需添加依赖项,如 layout_above 用于 Toolbarlayout_below 用于片段容器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接