如何使我的视图在Android上可滚动?

时间:2019-09-13 18:15:54

标签: java android android-studio user-interface scrollview

我创建了xml视图,并且在预览中看起来不错。但是当我在模拟器上玩时,元素无法修复,我认为模拟器太小。 因此,我尝试的解决方案是将滚动条设置为垂直:

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

但是它没有生效,我认为它仅适用于列表或长文本  The preview of my aplication The real view

5 个答案:

答案 0 :(得分:1)

您需要使用ScrollView作为父布局,如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">

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

    </LinearLayout>
</ScrollView>

答案 1 :(得分:0)

只需将当前代码包装到Scrollview中,如果文本太大或溢出,它将滚动。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollview"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</ScrollView>

请注意,我使用的是match_parent,这将占用所有可用空间,因此请仔细检查您是否不需要wrap_content之类的高度/宽度。

答案 2 :(得分:0)

我认为问题在于默认情况下,TextView只是单个行对象。如果要显示多行,则需要在xml布局中定义它们:

android:maxLines="10"
android:lines="5"

答案 3 :(得分:0)

尝试在您的xml中添加android:

layout_marginBottom="16dp"

与此相关的TextView应该从底部至少16dp或将其包装在

答案 4 :(得分:0)

为此,您需要将TextView放在ScrollView中。但是,请注意,一个ScrollView中只能有一个直接子级(https://developer.android.com/reference/android/widget/ScrollView.html)。如果需要在滚动视图中放置多个视图,可以将它们放置在LinearLayout中,然后将此LinearLayout放置在ScrollView中,就像Jagar指出的https://stackoverflow.com/a/57928644/12019759