滚动时如何使工具栏位于顶部? (Android Studio)

时间:2018-10-08 20:57:24

标签: xml android-studio scroll toolbar

我创建了一个toolbar,可以很好地进行导航。问题是,当我滚动toolbar时,它并不会固定在顶部。我滚开它。自然,我想一直保持在最高位置。我认为问题是我在toolbar层次结构中有scrollview xml。看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.nollvision.slothmode.nollvision.DetailActivity2">

<android.support.constraint.ConstraintLayout
    android:id="@+id/detail2_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:backgroundTint="@color/colorPrimaryDark"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <ImageView... AND SO ON....

我试图将结构更改为Constraint-> ScrollView-> Constraint-> children。像这样:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nollvision.slothmode.nollvision.DetailActivity2">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:backgroundTint="@color/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<android.support.constraint.ConstraintLayout
        android:id="@+id/detail2_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<ImageView...AN SO ON

结果是toolbar完全消失,并且布局到处都是。还是,这是正确的方法,还是有更好的,最重要的是更简便的方法?

1 个答案:

答案 0 :(得分:1)

您应该为此使用FrameLayout。

类似这样的东西:-

 <?xml version="1.0" encoding="utf-8"?>
    <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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nollvision.slothmode.nollvision.DetailActivity2">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:backgroundTint="@color/colorPrimaryDark"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
            android:id="@+id/detail2_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent">