【问题标题】:Fragment content is overlaying Toolbar片段内容覆盖工具栏
【发布时间】:2014-11-10 01:14:18
【问题描述】:

我最近开始研究 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>

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

【问题讨论】:

    标签: android android-fragments android-toolbar


    【解决方案1】:

    请不要忘记将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>
    

    【讨论】:

      【解决方案2】:

      使用RelativeLayout 而不是FrameLayout 作为最高父级。然后只需为Toolbar 添加layout_above 之类的依赖项,或者为片段容器添加layout_below 之类的依赖项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多