美文网首页自定义控件Android应用开发大全
Android-推荐一个自动调整文字大小的开源库(AutoFit

Android-推荐一个自动调整文字大小的开源库(AutoFit

作者: 阿博聊编程 | 来源:发表于2022-07-27 09:56 被阅读0次
    图片来源网络,入侵必删

    在日常的开发中,我们可能会遇到说根据容器自动调整文字大小的需求。这篇博客分享一个开源库帮大家快速实现,希望能帮看文章的小伙伴提高开发效率。

    AutoFitTextView

    一个实现自动调整文本大小以完全适应其边界的TextView的开源库。

    引入项目

    dependencies {
        implementation 'me.grantland:autofittextview:0.2.1'
    }
    

    截止我发博客,开源库版本是0.2.1,使用最新版本请看开源文档

    使用示例

    1、在代码里面使用:

    AutofitHelper.create(textView);
    

    2、XML的使用:

    <me.grantland.widget.AutofitLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"/>
    </me.grantland.widget.AutofitLayout>
    

    在代码中或者XML使用内置的Widget:

    <RootElement
        xmlns:autofit="http://schemas.android.com/apk/res-auto"
        ...
    
    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:maxLines="2"
        android:textSize="40sp"
        autofit:minTextSize="16sp"/>
    

    最后总结,这个开源库更新时间比较久远,如果使用最新的编译器的话,需要自己clone代码到项目当中修改使用。

    相关文章

      网友评论

        本文标题:Android-推荐一个自动调整文字大小的开源库(AutoFit

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