ScrollView未放置在ToolBar下方

问题描述 投票:0回答:3

我遇到了以下问题:我的活动中的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>

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

enter image description here

我不明白,为什么会发生这种情况,因为我把它放在工具栏下面,所以,这是怎么回事?

android xml toolbar android-constraintlayout
3个回答
1
投票

发生这种情况是因为您的滚动视图高度是match_parent而不是0dp - 因此您的滚动视图将不会尊重您的约束并将遍布整个屏幕。

请注意您使用的是tools:ignore="MissingConstraints",工具属性只会影响预览,因此您将以与预览不同的方式查看布局。

另外,你错过了一些约束 - 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>

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"
    >

这适用于我的应用程序。


0
投票

你可能会尝试:

 <LinearLayout>
      vertical
      <RelativeLayout>
           <Toolbar>
               someID
               parent top
               parent left
           <Scrollview>
               below someID
               parent left
© www.soinside.com 2019 - 2024. All rights reserved.