简体   繁体   English

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

[英]How to make my view scrollable on Android?

I create my xml view, and in the preview it seems good. 我创建了xml视图,并在预览中看起来不错。 But when i play on my emulator the elements doesn't fix, i think the emulator is too small. 但是当我在模拟器上玩时,元素无法修复,我认为模拟器太小。 So the solution i have tried is setting the scrollbars to vertical: 所以我尝试的解决方案是将滚动条设置为垂直:

<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" />

But it doesn´t take effect, i think it is only for lists or long texts The preview of my aplication The real view 但doesn't生效,我认为这是只对名单或长文我aplication的预览 真实看法

You need to use a ScrollView as the parent layout, something like the following: 您需要使用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>

Just wrap your current code into a Scrollview and it will scroll if the text is too large or overflow. 只需将当前代码包装到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>

Note that I am using match_parent which means it will take up all available space, so double check that you won't need something like wrap_content instead for the height / width. 请注意,我使用的是match_parent ,这意味着它将占用所有可用空间,因此请仔细检查您是否不需要诸如wrap_content之类的高度/宽度。

I think the problem is that by default, a TextView is only a single line object. 我认为问题在于默认情况下,TextView只是单个行对象。 If you want to show multiple lines, you need to define them in your xml layout: 如果要显示多行,则需要在xml布局中定义它们:

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

Try add android in your xml: 尝试在您的xml中添加android:

layout_marginBottom="16dp"

with this the TextView should be anleast 16dp from bottom or wrap it inside 与此TextView应该从底部至少16dp或包装在里面

You need to put your TextView inside a ScrollView for this. 为此,您需要将TextView放在ScrollView中。 However, take note that a ScrollView can only have one direct child inside it ( https://developer.android.com/reference/android/widget/ScrollView.html ). 但是,请注意,一个ScrollView只能在其中包含一个直接子代( https://developer.android.com/reference/android/widget/ScrollView.html )。 If you need to put more than one view inside a scroll view you can put them inside a LinearLayout and then put this LinearLayout inside the ScrollView, like Jagar pointed out https://stackoverflow.com/a/57928644/12019759 如果需要在滚动视图中放置多个视图,可以将它们放置在LinearLayout中,然后将此LinearLayout放置在ScrollView中,例如Jagar指出https://stackoverflow.com/a/57928644/12019759

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM