【问题标题】:Textview horizontal and vertical scrollingTextview水平和垂直滚动
【发布时间】:2020-08-17 15:16:51
【问题描述】:

我想要一个水平和垂直滚动的文本字段。屏幕底部是一个按钮。到目前为止它有效,但文本视图高度仅与内容相同。有没有办法生成父元素(滚动条)的高度?

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@+id/startStressTestBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="START StressTest"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <ScrollView
        android:id="@+id/main_tv_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="100dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/startStressTestBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/text_home"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="horizontal"
                android:scrollHorizontally="true"
                android:textSize="15sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </HorizontalScrollView>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

目标是滚动条位于按钮的底部顶部(match_parent)。所以我可以使用“整个”屏幕进行滚动。

【问题讨论】:

  • 你现在拥有什么,你想拥有什么?请附上图片
  • 请更好地描述您的问题?
  • 目前文本视图只有内容的高度,如“wrap_content”约束。您会看到水平滚动条直接位于文本下方。我希望 textview 的高度填满屏幕上的空白区域。

标签: android layout scroll textview


【解决方案1】:

不要把卷轴放在一起,一一设置约束,试试这个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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <ScrollView
        android:id="@+id/main_tv_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="100dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/startStressTestBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </ScrollView>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="100dp"
        app:layout_constraintBottom_toTopOf="@+id/startStressTestBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </HorizontalScrollView>
    <TextView
        android:id="@+id/text_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal"
        android:scrollHorizontally="true"
        android:textSize="15sp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="100dp"
        app:layout_constraintBottom_toTopOf="@+id/startStressTestBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <Button
        android:id="@+id/startStressTestBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="START StressTest"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 嗨,这里的 TextView 不再滚动 - 这就是我将 TextView 嵌套在 ScrollViews(水平、垂直)中的原因。
  • 您希望它垂直滚动还是水平滚动? @typhon
  • 是的,当然,这就是我将两个滚动视图都放在这个视图中的原因?这也是我的问题的第一句话。
  • 滚动不是我的问题——我不想让 Horizo​​ntalScrollView 在启动应用程序后填充空间——而不是在用文本填充滚动视图之后。
猜你喜欢
  • 2014-01-09
  • 1970-01-01
  • 2012-05-20
  • 2015-05-31
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多