ScrollView不在工具栏下方

时间:2019-04-29 19:36:53

标签: android xml toolbar android-constraintlayout

我遇到以下问题:我的活动中的ScrollView应该放在ToolBar下方。这是此活动的布局:

<?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=".SongLyricsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000FF"
        tools:ignore="MissingConstraints" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp">

        <TextView
            android:id="@+id/lyrics_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </ScrollView>



</android.support.constraint.ConstraintLayout>

但是当我运行该应用程序时,我看到了:

DRY

我不明白,为什么会发生这种情况,因为我将其放置在工具栏下方,怎么了?

3 个答案:

答案 0 :(得分:1)

我使用marginTop参数(等于工具栏高度)解决了这个问题,如下所示:

 <ScrollView
    android:id="@+id/activity_show_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="?attr/actionBarSize"
    android:background="@color/transparent_background"
    >

这适用于我的应用程序。

答案 1 :(得分:1)

之所以发生这种情况,是因为您的滚动视图高度是match_parent而不是0dp-因此,您的滚动视图不会遵守您的约束条件,并且会散布在整个屏幕上。 < / p>

请注意,您正在使用tools:ignore="MissingConstraints",并且tool属性只会影响预览,因此您将以与预览不同的方式看到布局。

此外,您缺少一些约束-app:layout_constraintTop_toTopOf="parent"

现在有了该约束,它应该可以工作:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0000FF"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginTop="16dp">

    <TextView
        android:id="@+id/lyrics_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

答案 2 :(得分:0)

您可以尝试:

 <LinearLayout>
      vertical
      <RelativeLayout>
           <Toolbar>
               someID
               parent top
               parent left
           <Scrollview>
               below someID
               parent left