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

我创建了 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" />

但它没有生效,我认为它只适用于列表或长文本 


明月笑刀无情
浏览 125回答 5
5回答

隔江千里

您需要使用 ScrollView 作为父布局,如下所示:<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:fillViewport="true">&nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center">&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; android:id="@+id/tv"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; </LinearLayout></ScrollView>

米脂

尝试在您的 xml 中添加 android:layout_marginBottom="16dp"这样 TextView 应该距底部至少 16dp 或将其包裹在里面

慕森王

我认为问题在于默认情况下,TextView 只是一个单行对象。如果你想显示多行,你需要在 xml 布局中定义它们:android:maxLines="10" android:lines="5"

慕森卡

只需将当前代码包装到 Scrollview 中,如果文本太大或溢出,它就会滚动。<ScrollView&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:id="@+id/scrollview"&nbsp; &nbsp; >&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tv"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:scrollbars="vertical"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintLeft_toLeftOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintRight_toRightOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent" /></ScrollView>请注意,我正在使用match_parent这意味着它将占用所有可用空间,因此请仔细检查您是否不需要类似的wrap_content高度/宽度。

萧十郎

为此,您需要将 TextView 放入 ScrollView 中。但是,请注意,ScrollView 内部只能有一个直接子级(https://developer.android.com/reference/android/widget/ScrollView.html)。如果您需要将多个视图放入滚动视图中,您可以将它们放入 LinearLayout 中,然后将此 LinearLayout 放入 ScrollView 中,就像 Jagar 指出的https://stackoverflow.com/a/57928644/12019759
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java