【问题标题】:How to make my view scrollable on Android?如何让我的视图在 Android 上可滚动?
【发布时间】:2019-09-13 18:15:54
【问题描述】:
我创建了我的 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
【问题讨论】:
标签:
java
android
android-studio
user-interface
scrollview
【解决方案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>
【解决方案2】:
只需将您当前的代码包装到 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 之类的东西来代替高度/宽度。
【解决方案3】:
我认为问题在于默认情况下,TextView 只是一个单行对象。如果要显示多行,则需要在 xml 布局中定义它们:
android:maxLines="10"
android:lines="5"
【解决方案4】:
尝试在你的xml中添加android:
layout_marginBottom="16dp"
这样,TextView 应该距离底部至少 16dp 或将其包裹在里面