Android之TextView自适应大小

发布时间 - 2026-01-10 23:24:17    点击率:

对于设置TextView的字体默认大小对于UI界面的好看程度是很重要的,小屏幕设置的文字过大或者大屏幕设置的文字过小都造成UI的不美观

现在就让我们学习自适应大小的TextView控件,即当文字长度变化时,文字的大小会相应的变化,保证显示在一行当中

实现依靠于第三方类库

第三方类来源:

https://github.com/grantland/android-autofittextview

和正常的使用TextView一样,只需要将要自适应的TextView标签设置为<me.grantland.widget.AutofitTextView/>

注意:一定要设置为单行,否定无法显示效果

android:singleLine="true"

<me.grantland.widget.AutofitTextView
   android:id="@+id/output_autofit"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/example"
   android:textSize="50sp"
   android:gravity="center"
   android:singleLine="true"
   autofit:minTextSize="8sp"
   />

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:autofit="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  >
  <EditText
   android:id="@+id/input"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:singleLine="true"
   android:hint="@string/input_hint"
   android:text="@string/example"/>
  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/label_normal"
   />
  <TextView
   android:id="@+id/output"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/example"
   android:textSize="50sp"
   android:gravity="center"
   />
  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/label_autofit"
   />
  <me.grantland.widget.AutofitTextView
   android:id="@+id/output_autofit"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/example"
   android:textSize="50sp"
   android:gravity="center"
   android:singleLine="true"
   autofit:minTextSize="8sp"
   />
 </LinearLayout>
</ScrollView>

activity_main.xml

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="app_name">Texttest</string>
 <string name="action_settings">Settings</string>
 <string name="hello_world">Hello world!</string>
 <string name="input_hint">text</string>
 <string name="label_normal">Normal:</string>
 <string name="label_autofit">Autofit:</string>
 <string name="example">This is an example</string>
</resources>

activity

package com.example.texttest;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
 private TextView mOutput;
 private TextView mAutofitOutput;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mOutput = (TextView)findViewById(R.id.output);
  mAutofitOutput = (TextView)findViewById(R.id.output_autofit);
  ((EditText)findViewById(R.id.input)).addTextChangedListener(new TextWatcher() {
   @Override
   public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    // do nothing
   }
   @Override
   public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    mOutput.setText(charSequence);
    mAutofitOutput.setText(charSequence);
   }
   @Override
   public void afterTextChanged(Editable editable) {
    // do nothing
   }
  });
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
}

MainActivity.java

效果:

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


# textview自适应大小  # textview  # 文字自适应  # 4种Android屏幕自适应解决方案  # Android编程实现WebView自适应全屏方法小结  # Android编程实现屏幕自适应方向尺寸与分辨率的方法  # Android中WebView图片实现自适应的方法  # Android提高之ListView实现自适应表格的方法  # Android中让图片自适应控件的大小的方法  # Android字体大小自适应不同分辨率的解决办法  # 第三方  # 设置为  # 自适应  # 很重要  # 只需要  # 过大  # 显示效果  # 就让我们  # 类库  # textSize  # version  # utf  # string  # encoding  # text  # gravity  # sp  # minTextSize  # autofit  # center 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  Python结构化数据采集_字段抽取解析【教程】  中国移动官方网站首页入口 中国移动官网网页登录  bootstrap日历插件datetimepicker使用方法  laravel怎么在请求结束后执行任务(Terminable Middleware)_laravel Terminable Middleware请求结束任务执行方法  Laravel如何实现RSS订阅源功能_Laravel动态生成网站XML格式订阅内容【教程】  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  简历没回改:利用AI润色让你的文字更专业  lovemo网页版地址 lovemo官网手机登录  如何在HTML表单中获取用户输入并用JavaScript动态控制复利计算循环  Laravel策略(Policy)如何控制权限_Laravel Gates与Policies实现用户授权  Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全  Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面  ,南京靠谱的征婚网站?  Python文件异常处理策略_健壮性说明【指导】  googleplay官方入口在哪里_Google Play官方商店快速入口指南  Laravel如何使用Service Provider服务提供者_Laravel依赖注入与容器绑定【深度】  JavaScript Ajax实现异步通信  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  如何在建站主机中优化服务器配置?  在Oracle关闭情况下如何修改spfile的参数  Laravel怎么进行数据库回滚_Laravel Migration数据库版本控制与回滚操作  如何挑选优质建站一级代理提升网站排名?  Android Socket接口实现即时通讯实例代码  Laravel怎么自定义错误页面_Laravel修改404和500页面模板  浏览器如何快速切换搜索引擎_在地址栏使用不同搜索引擎【搜索】  android nfc常用标签读取总结  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  Android利用动画实现背景逐渐变暗  Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程  Laravel如何构建RESTful API_Laravel标准化API接口开发指南  laravel怎么用DB facade执行原生SQL查询_laravel DB facade原生SQL执行方法  在centOS 7安装mysql 5.7的详细教程  Laravel怎么使用artisan命令缓存配置和视图  Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置  网站页面设计需要考虑到这些问题  Microsoft Edge如何解决网页加载问题 Edge浏览器加载问题修复  微信推文制作网站有哪些,怎么做微信推文,急?  Win11关机界面怎么改_Win11自定义关机画面设置【工具】  php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】  极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?  如何在HTML表单中获取用户输入并结合JavaScript动态控制复利计算循环  青岛网站建设如何选择本地服务器?  东莞市网站制作公司有哪些,东莞找工作用什么网站好?  香港网站服务器数量如何影响SEO优化效果?  如何正确选择百度移动适配建站域名?  Linux安全能力提升路径_长期防护思维说明【指导】  EditPlus中的正则表达式 实战(1)  如何快速生成专业多端适配建站电话?  Windows Hello人脸识别突然无法使用