【问题标题】:Android - FrameLayout is overlapped by CoordinatorLayout when use NestedScrollView inside the FragmentAndroid - 在 Fragment 内使用 NestedScrollView 时,FrameLayout 与 CoordinatorLayout 重叠
【发布时间】:2016-11-11 17:15:15
【问题描述】:

我正在尝试使 CoordinatorLayout 包含一个 FrameLayout 作为子级。这个 FrameLayout 稍后会被替换为以 NestedScrollView 作为父级的 Fragment。

问题在于,当创建片段时,FrameLayout 处于错误的位置,就像它有某种边距一样。

在下一张图片中,我将向您展示初始位置。

Initial position

一开始应该是这样的:

Right position

这是活动的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/gray">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="340dp"
        android:orientation="vertical">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/ctlLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <TextView
                    android:id="@+id/beer_style"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="European Lagers"
                    android:textColor="@color/gray"
                    android:textSize="16sp"/>

                <TextView
                    android:id="@+id/beer_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Birra Peroni"
                    android:textColor="@color/white"
                    android:textSize="24sp"/>

                <de.hdodenhof.circleimageview.CircleImageView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/beer_image"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:layout_margin="16dp"
                    android:src="@mipmap/logo"
                    app:civ_border_width="2dp"
                    app:civ_border_color="#FF000000"/>

                <com.iarcuschin.simpleratingbar.SimpleRatingBar
                    android:id="@+id/rating_bar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srb_starSize="28dp"
                    app:srb_numberOfStars="5"
                    app:srb_stepSize="0.5"
                    app:srb_borderColor="@color/black"
                    app:srb_fillColor="@color/gray"
                    app:srb_starCornerRadius="20"/>

            </LinearLayout>

            <!-- Toolbar -->
            <include android:id="@+id/toolbar"
                layout="@layout/toolbar" />
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        app:behavior_overlapTop="60dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

这是我在 FrameLayout 中替换的布局

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="jdeveloper.coriassos.views.fragments.BeerDetailFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="30dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/corner_layout"
        android:orientation="vertical"
        android:layout_margin="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/information"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="8dp"/>

        <TextView
            android:id="@+id/beer_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lore_ipsum"
            android:textColor="@color/gray"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="right"
            android:layout_margin="16dp">

            <TextView
                android:id="@+id/beer_origin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="California, USA"
                android:textColor="@color/gray"/>

            <TextView
                android:id="@+id/beer_abv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Alc. 5,00%"
                android:textColor="@color/gray"/>

        </LinearLayout>

    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="1dp"
        android:background="@drawable/corner_top_layout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/available"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:layout_margin="16dp"/>

    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/variant_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp" />

</LinearLayout>

【问题讨论】:

    标签: android android-fragments android-coordinatorlayout nestedscrollview


    【解决方案1】:

    减小值

    app:behavior_overlapTop="60dp" 
    

    在您的 FrameLayout 中

    【讨论】:

    • 这不是解决方案,也许我必须编辑帖子。如果您查看“初始位置”图像,您会发现布局突然位于工具栏下方。查看顶角,应该有第二张图片中的半径。
    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多