Android TextView,EditText要求固定行数自动调整TextSize
最近项目有个需求要求文本最多显示3行,继续输入则字体变小,删除已经输入的文字,那么字体变大,不管变大变小都不能超过3行。网上怎么找也找不到相关的解决方案,自己动手,丰衣足食了!
说一下算法思路,后面给出demo。
第一步 获取当前的行数
我们需要知道目前文本多少行了,之前我采用TextView.getPaint().measureText("your text")这种方法来获取文字的总长度,然后再除以每行的宽度,得到行数,其实这个算法也是可行,不过我采用更简单的方法了。采用view.post方法里面可以通过lineCount = textView.getLineCount();获取正确的行数,否则getLineCount()一直等于0。
第二步 训练小于多少个字数的时候使用多大的字体
比如0~30个字数的时候,字体大小是50sp不会超过3行,31~50个字数的时候,字体大小是40sp不会超过3行,51~80个字数的时候,字体大小是30sp不会超过3行。那么这个数据需要记录,因为,删除文字的时候,删到哪个位置需要知道改用多大的字体。而且取得当前当前位置使用多大的字体的时候,需要删除这个字数和字体大小对应关系的数据,因为,这个数据,我们需要不断的训练更新,因为大小写,字符,不同的语言,得到到文字length是不同的。
有了上面两步的分析,我们就可以动态的调整文字的TextSize了。
给出完整的Demo
1、页面设计,简单的就一个输入框和一个文本框,输入框输入上面文本框就显示什么
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.figo.study.activity.TextActivity"> <TextView
android:id="@+id/txt_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/et_msg"
android:text="@string/hello_world"
android:textColor="#ffffff" /> <EditText
android:id="@+id/et_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="141dp" /> </RelativeLayout>
2、Activity编写
package com.figo.study.activity; import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import com.figo.study.R;
import com.figo.study.utils.CommonUtil;
import java.util.ArrayList; /**
* 控制在3行,自动调整textSize
*/
public class TextActivity extends Activity {
TextView mTxt;
EditText mEt;
int mDefaultTextSize = ;
int lineCount = ;
int lastTextLength = ;
boolean isMinus = false;
int maxTextSize = ;
int minTextSize = ;
ArrayList<PositionTextSize> arrayListPts = new ArrayList<>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
initView();
} private void initView() {
mTxt = (TextView) findViewById(R.id.txt_msg);
mTxt.setMaxWidth();
mTxt.setTextSize(mDefaultTextSize);
mEt = (EditText) findViewById(R.id.et_msg);
mEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override
public void afterTextChanged(Editable s) {
mTxt.setText(s.toString());
resizeTextSize(mTxt);
}
});
} private void resizeTextSize(final TextView textView) {
textView.post(new Runnable() {
@Override
public void run() {
TextPaint textPaint = textView.getPaint();
float currentTextSize = CommonUtil.px2sp(TextActivity.this, textPaint.getTextSize());
float newTextSize = currentTextSize; lineCount = textView.getLineCount();
int currentTextLength = textView.getText().length();
if (currentTextLength > lastTextLength) {
isMinus = false;
} else {
//说明是在减
isMinus = true;
}
lastTextLength = currentTextLength; if (lineCount > ) {
if (currentTextSize > minTextSize) {
newTextSize = currentTextSize - ;
}
PositionTextSize pts = new PositionTextSize();
pts.textSize = currentTextSize;
pts.position = currentTextLength;
arrayListPts.add(pts); }
if (isMinus) {
newTextSize = findPositionTextSize(currentTextLength);
}
if ((newTextSize != currentTextSize) && newTextSize > ) {
textView.setTextSize(newTextSize);
}
Log.i("TextActivity", "getLineCount:" + lineCount);
Log.i("TextActivity", "textSize:" + currentTextSize);
Log.i("TextActivity", "textLength:" + currentTextLength);
Log.i("TextActivity", "ArraryTextSize:" + arrayToString(arrayListPts));
} });
} private String arrayToString(ArrayList<PositionTextSize> arrPts) {
StringBuffer buffer = new StringBuffer();
for (PositionTextSize pts : arrPts) {
buffer.append("position:" + pts.position);
buffer.append(",textSize:" + pts.textSize + "|");
}
return buffer.toString();
} private float findPositionTextSize(int position) {
float textSize = ;
PositionTextSize result = null;
int size = arrayListPts.size();
for (int p = size - ; p >= ; p--) {
if (arrayListPts.get(p).position > position) {
result = arrayListPts.get(p);
textSize = result.textSize;
arrayListPts.remove(result);//删除重新训练
}
}
return textSize;
} public class PositionTextSize {
int position;
float textSize;
}
}
Android TextView,EditText要求固定行数自动调整TextSize的更多相关文章
- EditText 几种显示方式,固定行数,自适应行数
1.显示7行,超过7行自动向下补充行数 <EditText android:id="@+id/edt_content" android:layout_width=" ...
- Grid++Report设置显示固定行数
一.要实现的功能打印的报表显示固定的行数,并且设置字段的文字可以自动换行二.设置步骤1.鼠标左键单击“明细网格”栏,在右侧属性窗口中设置“追加空白行”属性值为:是:“追加空白行在后”属性值为:是.2. ...
- FastReport之实现打印固定行数,不足补打空白行的办法
在设置单据的打印模板的时候,我们有时候会遇到这样的情况:单据的内容很少,打印出来的效果不理想的情况,例如1.单据体与单尾之间有大量的空白: 2.单据体跟单尾连在一起,单尾后面的空白篇幅太大: 以上这两 ...
- oracle取随机数,取固定行数的数
首先建一张测试表: create table DIM_IA_TEST5 ( NAME ), OTHERNAME ), NUM NUMBER, TIMES NUMBER ) 然后插入数据,现在的表数据为 ...
- Oracle 行转列(不固定行数的行转列,动态)(转)
http://bbs.csdn.net/topics/330039676 SQLSERVER :行列转换例子: http://www.cnblogs.com/gaizai/p/3753296.htm ...
- 纯css实现不固定行数的文本在一个容器内垂直居中
项目中要实现的效果如图: html代码 及 css代码: <!DOCTYPE html> <html> <head> <meta charset=" ...
- python pandas使用chunksize异步拆分固定行数的文件
import pandas as pd import asyncio from collections import defaultdict collect = defaultdict(list) # ...
- Android studio如何显示代码行数
1.首先打开as:File-->Settings... 2.Editor-->General-->Appearence 3.右边show line numbers 4.依次点击app ...
- Android中获取TextView行数
项目中发现,如果直接通过TextView.getLineCount()方法获取行数时,总是0,研究发现,setText()后立即调用getLineCount(), 这时TextView还未完成meas ...
随机推荐
- MariaDB 安装 (YUM)
在CentOS 7.0安装MariaDB的数据库,在这里记录下安装过程,以便以后查看. 1. 安装MariaDB 安装命令 yum -y install mariadb mariadb-server ...
- 【例题 8-10 UVA - 714】 Copying Books
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二分最后的最大值的最小值. 得到ans 然后从后往前尽量划分. 如果发现不够分成k个. 那么就从第一个开始接着分restk个(每隔1 ...
- Servlet 规范笔记—基于http协议的servlet
在上一章节,我们大概的描述了servlet的规范以及servlet和servlet容器的概念和用途,我们清楚的知道servlet容器提供了接收来自client端的请求,然后根据请求进行处理(如:执行对 ...
- Detecting a return-oriented programming exploit
A method and apparatus for detecting a Return-Oriented Programming exploitation. At a computer devic ...
- Method of address space layout randomization for windows operating systems
A system and method for address space layout randomization ("ASLR") for a Windows operatin ...
- System.out.println 的多线程并发问题
假设println函数的參数为常量则不会出现线程并发问题,可是假设參数为表达式形式.则JVM在运行println函数的时候会分为几步来运行,从而造成并发问题. 例如以下样例所看到的: package ...
- 51 nod 1189 阶乘分数
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1189 题目思路: 1/n! = 1/x +1/y ==> ...
- BZOJ——3343: 教主的魔法 || 洛谷—— P2801 教主的魔法
http://www.lydsy.com/JudgeOnline/problem.php?id=3343 || https://www.luogu.org/problem/show?pid=280 ...
- 2015,我的投资理财策略(股权众筹+P2P网贷+活期理财)
纸币流行,尤其是当今中国的市场经济,纸币几乎是一直是贬值的,每个人的财富都在被不断地稀释,可能是被政府.如果你不注意保值增值,你就越来越穷. 当年的万元户,在今天看来就是一个笑话,其实不怎么好 ...
- QWaitCondition 的正确使用方法(通过 mutex 把有严格时序要求的代码保护起来,同时把 wakeAll() 也用同一个 mutex 保护起来)
简单用法 QWaitCondition 用于多线程的同步,一个线程调用QWaitCondition::wait() 阻塞等待,直到另一个线程调用QWaitCondition::wake() 唤醒才继续 ...