美文网首页
AutoresizeTextView为什么无法自适应大小

AutoresizeTextView为什么无法自适应大小

作者: zbmzly | 来源:发表于2017-09-01 13:46 被阅读0次

AutoresizeTextView是github上实现TextView中文字随控件大小自动缩放的控件。但是实际使用中,会出现字体大小无法自适应的情况。这个时候,需要注意一下控件属性的设置是否出现了问题。比如在TableRow中,你往往会使用这种形式的代码:

<TableRow
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@color/word_black">

            <com.****.AutoResizeTextView
                android:id="@+id/firstcol"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="1dp"
                android:layout_weight="2.3"
                android:background="@color/background_white"
                android:gravity="center"
                android:padding="6dp"
                android:textColor="@color/word_black"
                android:textSize="@dimen/text_little_size" />
.....

你需要注意了,这段代码指定的android:layout_height="match_parent"其实是一种自适应的格式,也就是说这个TextView能有多高他就有多高,从而无法实现字体自适应。这个时候,你添加一个maxHeight属性就很有必要,TextView会限制其自身大小,从而控件在layout passing的时候,您会得到正确的measuredHeight,像这样:

     <TableRow
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@color/word_black">

            <com.***.AutoResizeTextView
                android:id="@+id/firstcol"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="1dp"
                android:maxHeight="50dp"
                android:layout_weight="2.3"
                android:background="@color/background_white"
                android:gravity="center"
                android:padding="6dp"
                android:textColor="@color/word_black"
                android:textSize="@dimen/text_little_size" />

当然,一般性来说,使用固定大小的dp或者px值就可以解决问题。这里只是需要有些变通罢了。

相关文章

网友评论

      本文标题:AutoresizeTextView为什么无法自适应大小

      本文链接:https://www.haomeiwen.com/subject/uyahjxtx.html