【问题标题】:Android TextView doesn't "wrap_content" when setting text设置文本时,Android TextView 不会“wrap_content”
【发布时间】:2020-10-30 14:00:14
【问题描述】:

我有一个活动,其中有一个 ViewPager2 和一个 TextView 在它下面。 滚动浏览ViewPager2 时,我使用registerOnPageChangeCallback 相应地设置TextView 的文本。

当我第一次启动活动时,registerOnPageChangeCallback 被触发,其中第一个元素的文本正确设置为TextView。但是,高度不会相应调整,即使布局将其设置为wrap_contentTextView 停留在基本上只有一行高度的状态,因此任何足以占据新行的文本都会被剪短,因为TextView 不够大,无法显示。

它的底层ConstraintLayout 也是如此,我发现它使用不同的背景颜色。

一旦我开始滑动ViewPager2,再次调用registerOnPageChangeCallback,文本会相应更改,这一次TestView的高度调整正确。

我尝试在设置文本后调用forceLayout() 或为TextView 设置android:scrollHorizontally="false",但它没有任何改变。

当我设置android:lines="3" 时,它将 TextView 的初始大小扩大到三行的高度,但显然这也没有调整。设置例如android:maxLines="6" 什么都不做。

布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00A">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintDimensionRatio="4:3"
            />

        <TextView
            android:id="@+id/descriptionTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/viewPager2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="text that is way too long to fit into one line and thereby has to have two lines"
            android:background="#0A0"
            android:textColor="@color/primaryTextColor"
            android:textSize="18sp"
            android:gravity="center"
            android:layout_gravity="center"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

onPageChangeCallback

viewPager2.registerOnPageChangeCallback(
            object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    // getting my data

                    descriptionTextView.text = data.description
                }
            }
        )

另一个奇怪的事情是,当我删除android:textSize="18sp" 时,文本视图根本不会调整大小。于是出现了以下行为:

  1. 删除 android:textSize="18sp" -> TextView 根本不调整大小
  2. 保持android:textSize="18sp" -> TextView 仅在第一次滑动ViewPager2 后调整大小

这种行为对我来说很奇怪,我不明白这是怎么发生的,或者我该如何解决它。任何关于为什么会发生这种行为或我可以做些什么来始终让 TextView 从一开始就正确调整自身大小的任何信息都将不胜感激。

编辑 我尝试删除 ScrollView 并让 ConstraintLayout 成为根元素。同样的事情也会发生。这意味着它是 TextView 不会调整大小,即使它的父级有足够的空间。

【问题讨论】:

    标签: android android-layout kotlin textview


    【解决方案1】:

    ViewPager2 不支持 wrap_content,这会导致所有页面的高度与分页器中第一页的高度相同。您需要编写一个自定义 ViewPager 来覆盖 onMeasure 并且需要添加额外的逻辑来计算页面的高度并进行设置。

    参考链接:https://medium.com/winkl-insights/how-to-have-a-height-wrapping-viewpager-when-images-have-variable-heights-on-android-60b18e55e72e

    【讨论】:

    • 你误解了这个问题。我不想调整viewpager的高度,我想调整viewpager之外的TextView的高度
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2012-04-18
    • 2012-09-13
    相关资源
    最近更新 更多