【问题标题】:Android - How to auto size TextView to fit within a fixed height container?Android - 如何自动调整 TextView 的大小以适合固定高度的容器?
【发布时间】:2021-02-10 16:28:29
【问题描述】:

这就是我想要实现的目标:

基本上,我希望我的 TextView 仅相对​​于 TextView 容器的固定高度而不是文本长度自动调整大小。这是否只能在 XML 中实现(没有 Kotlin)?

当前实现:

 <androidx.appcompat.widget.AppCompatTextView
            style="@style/TextSFontMd"
            android:layout_width="0dp"
            android:layout_height="@dimen/_16sdp"
            android:ellipsize="end"
            android:lines="1"
            android:text="@{text}"
            app:autoSizeStepGranularity="@dimen/_1ssp"
            app:autoSizeTextType="uniform"
            tools:maxLength="10"
            tools:text="@string/placeholder_lorem_ipsum_short" />

【问题讨论】:

    标签: android xml android-layout textview


    【解决方案1】:

    希望对你有用

     <androidx.appcompat.widget.AppCompatTextView
                    style="@style/TextSFontMd"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/_16sdp"
                    android:ellipsize="end"
                    android:lines="1"
                    android:text="@{text}"
                    android:textSize="@dimen/_12ssp"
                    tools:maxLength="10"
                    tools:text="@string/placeholder_lorem_ipsum_short" />
    

    【讨论】:

    • 我想过这个,但是有没有可能在一些愚蠢的设备上高度会被奇怪地缩放并且文本会出现剪切?
    • 我认为它会起作用,因为您使用 SDP 和 SSP 作为大小,而这两个库都用于自动调整大小
    • 好的,谢谢,我会保留文本大小和视图高度之间的 4sdp 差异,以防万一。
    猜你喜欢
    • 2013-03-01
    • 2013-06-05
    • 1970-01-01
    • 2011-03-03
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多