AutoresizeTextView为什么无法自适应大小

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值就可以解决问题。这里只是需要有些变通罢了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容