为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

明月笑刀无情 2023-07-28 16:27:08
我创建了 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" />但它没有生效,我认为它只适用于列表或长文本 
查看完整描述

5 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

您需要使用 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>


查看完整回答
反对 回复 2023-07-28
?
米脂

TA贡献1836条经验 获得超3个赞

尝试在您的 xml 中添加 android:

layout_marginBottom="16dp"

这样 TextView 应该距底部至少 16dp 或将其包裹在里面


查看完整回答
反对 回复 2023-07-28
?
慕森王

TA贡献1777条经验 获得超3个赞

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

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


查看完整回答
反对 回复 2023-07-28
?
慕森卡

TA贡献1806条经验 获得超8个赞

只需将当前代码包装到 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高度/宽度。


查看完整回答
反对 回复 2023-07-28
?
萧十郎

TA贡献1815条经验 获得超12个赞

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


查看完整回答
反对 回复 2023-07-28
  • 5 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信