ScrollView无法在Fragment

时间:2017-12-15 01:08:03

标签: android android-layout android-fragments android-scrollview android-constraintlayout

我有一个ScrollView和一个带有一些视图的ConstraintLayout,但它没有滚动。我在其他相关问题中看到了一些建议:

android:fillViewport="true"

到布局,我曾尝试使用NestedScrollView并更改ScrollView的位置,但没有任何帮助。

我的片段布局是:

<ScrollView
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >

    <android.support.constraint.ConstraintLayout
        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/layout_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:paddingEnd="?attr/listPreferredItemPaddingRight"
        android:paddingStart="?attr/listPreferredItemPaddingLeft"
        android:paddingTop="?attr/listPreferredItemPaddingLeft"


        >


        <TextView
            android:id="@+id/lbl_style"
            style="@style/HeadSection"
            android:text="@string/label_style"
            app:layout_constraintStart_toStartOf="@+id/guideline_label_margins"
            app:layout_constraintTop_toBottomOf="@+id/txt_beer_description"/>

        <TextView
            android:id="@+id/lbl_short_style"
            style="@style/Label"
            android:text="@string/label_short_style"
            app:layout_constraintStart_toStartOf="@+id/guideline_label_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_style_name"/>

        <TextView

            android:id="@+id/txt_short_style"
            style="@style/Content"
            app:layout_constraintStart_toStartOf="@+id/guideline_content_margins"
            app:layout_constraintTop_toBottomOf="@+id/txt_style_name"
            tools:text="American Dark Lager"/>


        <TextView
            android:id="@+id/lbl_style_name"
            style="@style/Label"
            android:text="@string/label_style_name"
            app:layout_constraintStart_toStartOf="@+id/guideline_label_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_style"/>

        <TextView
            android:id="@+id/txt_style_name"
            style="@style/Content"
            app:layout_constraintStart_toStartOf="@+id/guideline_content_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_style"
            tools:text="American-Style Dark Lager"/>


        <TextView
            android:id="@+id/lbl_style_category_name"
            style="@style/Label"
            android:layout_height="19dp"
            android:text="@string/label_style_category_name"
            app:layout_constraintStart_toStartOf="@+id/guideline_label_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_short_style"/>

        <TextView
            android:id="@+id/txt_style_category_name"
            style="@style/Content"
            app:layout_constraintStart_toStartOf="@+id/guideline_content_margins"
            app:layout_constraintTop_toBottomOf="@+id/txt_short_style"
            tools:text="North American Lager"/>


        <TextView
            android:id="@+id/lbl_beer"
            style="@style/HeadSection"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="4dp"
            android:text="@string/label_detail_head_beer_info"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/lbl_beer_name"
            style="@style/Label"
            android:text="@string/label_detail_beer_name"
            app:layout_constraintStart_toStartOf="@+id/guideline_label_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_beer"/>

        <TextView
            android:id="@+id/txt_beer_name"
            style="@style/Content"
            app:layout_constraintStart_toStartOf="@+id/guideline_content_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_beer"
            tools:text="Heineken"/>

        <TextView
            android:id="@+id/lbl_beer_Description"
            style="@style/Label"
            android:text="@string/label_detail_beer_description"
            app:layout_constraintStart_toStartOf="@+id/guideline_label_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_beer_name"/>

        <TextView
            android:id="@+id/txt_beer_description"
            style="@style/Content"
            android:layout_width="0dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/guideline_content_margins"
            app:layout_constraintTop_toBottomOf="@+id/lbl_beer_Description"/>


        <android.support.constraint.Guideline
            android:id="@+id/guideline_label_margins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_begin="16dp"/>

        <android.support.constraint.Guideline
            android:id="@+id/guideline_content_margins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_begin="120dp"/>


    </android.support.constraint.ConstraintLayout>
</ScrollView>

此布局在以下片段中膨胀:

content_detail.xml:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_detail"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    />

根布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/layout_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_height_huge"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                />


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

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


    <include
        layout="@layout/content_detail"/>


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

- 编辑

有关于此行为的bug report

我发现了一个应该修复的错误,但其他一些用户仍然报告了这个问题:

https://issuetracker.google.com/issues/37115702

- 编辑

我旋转手机,txt_beer_description TextView离开了屏幕。因此,如果我滚动滚动工作直到txt_beer_description结束,不再。

- 编辑

样式文件是:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@android:color/black</item>


    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>


    <style name="SearchBeerItemBaseText">
        <item name="android:gravity">start|center_vertical</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginStart">8dp</item>
    </style>

    <style name="SearchBeerItemTextPrimary" parent="SearchBeerItemBaseText">
        <item name="android:textColor">@color/primary_text</item>
        <item name="android:textSize">16sp</item>
    </style>

    <style name="SearchBeerItemTextSecundary" parent="SearchBeerItemBaseText">
        <item name="android:textColor">@color/secondary_text</item>
        <item name="android:textSize">14sp</item>
    </style>

    <style name="CollectionItemBaseText">
        <item name="android:gravity">start|center_vertical</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>


    <style name="CollectionItemTextPrimary" parent="CollectionItemBaseText">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">15sp</item>
        <item name="android:focusable">false</item>
        <item name="android:focusableInTouchMode">false</item>
        <item name="android:clickable">false</item>

    </style>

    <style name="CollectionItemTextSecundary" parent="CollectionItemBaseText">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">13sp</item>
    </style>


    <style name="SearchActivityPrimaryImage">
        <item name="android:layout_width">56dp</item>
        <item name="android:layout_height">56dp</item>
    </style>

    <style name="HeadSection">
        <item name="android:layout_marginTop">16dp</item>
        <item name="android:layout_marginBottom">8dp</item>
        <item name="android:textStyle">bold</item>

        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/primary</item>
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Subhead</item>
    </style>

    <style name="Label">
        <item name="android:textStyle">bold</item>
        <item name="android:layout_marginTop">16dp</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/primary_text</item>
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
    </style>

    <style name="Content" >
        <item name="android:layout_marginTop">16dp</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/secondary_text</item>
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>

    </style>


    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>




</resources>

2 个答案:

答案 0 :(得分:1)

ScorllView中,将layout_height更改为: android:layout_height="wrap_content"

答案 1 :(得分:1)

问题与appbar有关:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_height_huge"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

我不应该使用app:layout_behavior。