我在 Android 应用程序中遇到 TextView 高度调整问题。当我选择一个数量时,仅显示“Tamara”并且视图显示正确。但是,当我将数量更改为三个时,“Tabby”和“Tamara”均已启用并完美显示。当我将数量改回 1 时,问题就出现了;当“Tabby”和“Tamara”都可见时,高度似乎是基于之前的选择。
fun adjust() {
// Check if both views are visible
val bothViewsVisible = binding.tabbyFrameLayout.isVisible && binding.tamaraFrameLayout.isVisible
if (bothViewsVisible) {
binding.tamaraMessageTv.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Remove the listener to avoid multiple calls
binding.tamaraMessageTv.viewTreeObserver.removeOnGlobalLayoutListener(this)
// Measure the height of both TextViews
val height1 = binding.tamaraMessageTv.height
val height2 = binding.tabbyMessageTv.height
// Get the maximum height of both TextViews
val maxHeight = max(height1, height2)
// Set the height of both TextViews to the maximum height
binding.tabbyMessageTv.height = maxHeight
binding.tamaraMessageTv.height = maxHeight
}
})
} else {
binding.buyNowPayLaterContainer.requestLayout()
Log.d("", "layoutHeight:: ${binding.tamaraMessageTv.height}")
}
}
这是 XML 布局
`
<data>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_10dp"
android:visibility="gone"
tools:visibility="visible"
android:id="@+id/buyNowPayLaterContainer"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/tabbyFrameLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:minHeight="@dimen/dimen_40dp"
android:layout_weight="1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/bg_border_grey"
android:layout_marginTop="@dimen/dimen_8dp"
android:paddingStart="@dimen/dimen_12dp"
android:paddingEnd="@dimen/dimen_7dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/tabbyArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/tabbyMessageTv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tabbyMessageTv"
app:srcCompat="@drawable/ic_arrow_with_padding" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tabbyMessageTv"
style="@style/Italian_plate_no2_expanded_very_dark_grey_10sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_20dp"
android:layout_marginBottom="@dimen/dimen_10dp"
android:layout_marginEnd="@dimen/dimen_7dp"
android:ellipsize="end"
android:maxLines="3"
android:textSize="@dimen/dimen_12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tabbyArrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Pay 4 interest-free payments of AED xx.xx, Pay 4 interest-free payments of AED xx.xx" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/dimen_47dp"
android:layout_height="@dimen/dimen_20dp"
android:layout_marginStart="@dimen/dimen_12dp"
android:elevation="@dimen/dimen_6dp"
app:srcCompat="@drawable/bg_tabby" />
</FrameLayout>
<View
android:id="@+id/view"
android:layout_width="@dimen/dimen_8dp"
android:layout_height="@dimen/dimen_65dp" />
<FrameLayout
android:id="@+id/tamaraFrameLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/bg_white_rounded_tamara_border"
android:layout_marginTop="@dimen/dimen_8dp"
android:paddingStart="@dimen/dimen_12dp"
android:paddingEnd="@dimen/dimen_7dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/tamaraArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
app:layout_constraintBottom_toBottomOf="@+id/tamaraMessageTv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tamaraMessageTv"
app:srcCompat="@drawable/ic_arrow_with_padding" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tamaraMessageTv"
style="@style/Italian_plate_no2_expanded_very_dark_grey_10sp"
android:layout_width="@dimen/dimen_0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_20dp"
android:layout_marginBottom="@dimen/dimen_10dp"
android:layout_marginEnd="@dimen/dimen_7dp"
android:paddingEnd="@dimen/dimen_2dp"
android:textSize="@dimen/dimen_12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tamaraArrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Pay 4 interest-free payments of AED xx.xx" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/dimen_47dp"
android:layout_height="@dimen/dimen_20dp"
android:layout_marginStart="@dimen/dimen_12dp"
android:id="@+id/tamaraImageView"
android:elevation="@dimen/dimen_6dp"
app:srcCompat="@drawable/tamara_icon" />
</FrameLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
`
如何保证TextView的高度根据选择的数量动态调整,使得数量变化时只有必要的TextView可见,而不会出现多余的空间?
预先感谢您的任何见解或建议!
我在代码和 XML 布局中尝试了各种方法,但都没有解决问题。
有人可以提供指导或建议解决方案,以确保 TextView 高度根据所选数量动态调整,在数量变化时仅显示必要的 TextView,而没有任何额外空间吗?
您始终可以手动计算尺寸,然后使用正确的事件更新尺寸。