【问题标题】:Put a TextView with a ScrollView between two other views (header and footer)在其他两个视图(页眉和页脚)之间放置一个带有 ScrollView 的 TextView
【发布时间】:2020-10-28 19:56:33
【问题描述】:

我想在 ScrollView 中有一个可滚动的 TextView / TextView。

我自己已经尝试过并查找了多个页面,但是我的问题始终是显示“某些文本”的文本“文本”TextView 总是超出其他两个视图之间的空间,尽管进入“滚动模式”

这是我的 XML 代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="45dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:gravity="center_horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    tools:targetApi="jelly_bean" />

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginBottom="32dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintEnd_toEndOf="@+id/title"
    app:layout_constraintStart_toStartOf="@+id/title"
    app:layout_constraintTop_toBottomOf="@+id/title">

    <TextView
        android:id="@+id/middleText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="some text"
        tools:targetApi="jelly_bean" />
</ScrollView>

<LinearLayout
    android:id="@+id/linearLayout">

... 

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

更新

【问题讨论】:

  • ScrollView应该是父视图,你在找NestedScrollView
  • 谢谢,这就是我的想法。我已经在一些谷歌搜索上看到了 NestedScrollView,但从未点击过任何结果。我试试看,非常感谢

标签: android android-layout textview scrollview android-scrollview


【解决方案1】:

设置TextView 的这个android:scrollbars = "vertical" 属性也会放松不合适的textview 周围的约束属性。这里我做了一些改变,这使得 textview 可以滚动:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/title"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="45dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:gravity="center_horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        tools:targetApi="jelly_bean" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="344dp"
        android:layout_height="50dp"
        android:layout_marginTop="32dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title">

        <TextView
            android:id="@+id/middleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:text="some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text"
            tools:targetApi="jelly_bean" />
    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="32dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrollView"
        tools:ignore="MissingConstraints">


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

【讨论】:

  • 嗨 Ruben,感谢您的快速回复 :) 我已经在考虑将高度设置为固定值,但是有没有办法让它在用户屏幕上动态显示?
  • 当然这就是为什么ConstraintLayout你必须知道你到底想要什么并据此设置约束
  • 再次嗨,您是否可以使用这样的代码更新您的解决方案,使每个视图都保持在屏幕上,并在切换到“滚动模式”之前使带有大文本的 TextView 尽可能大?然后我会将您的解决方案标记为已接受,因为这正是我想要的:)
  • 我已经尝试过自己做,但是它从来没有像它应该的那样工作:/
  • 您的描述不清楚您想要什么。你能画出应该是什么样子的图表吗?
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多