http://www.cnblogs.com/psuwgipgf/p/4874158.html

一、 TextView字体随大小变化自适应TextView

实现依靠于第三方类库

第三方类来源:

http://www.apkbus.com/android-240301-1-1.html

下载地址:

http://yunpan.cn/cFs9qKMyFNyfy (提取码:b039)

使用时先导入类库

以eclipse为例

1、File-Import-Android-Existing Android Code Into Workspace

将第三方类库文件导入

2、右键类库 -properties 设置 is Library

3、创建个人项目,添加第三方类库

  右键项目-properties-Android-Add 选择类库 OK

然后就是正常的使用TextView了,只需要将要自适应的TextView标签设置为<me.grantland.widget.AutofitTextView/>

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

android:singleLine="true"
布局文件中的属性 布局文件
 <?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

activity_main

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>

String.xml

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

MianActivity

最新文章

  1. Java控制图片按比例缩放- (注意内存释放)
  2. 汉王云名片识别(SM)组件开发详解
  3. jekyll bootstrap
  4. RedHat 5 配置CentOS yum 更新源
  5. nodejs redis
  6. C++中的冒泡排序,选择排序,插入排序
  7. window.onbeforeunload 如果取消, 那么javascript变量会保存
  8. eclipse 使用指南
  9. Arrays.fill方法的陷阱
  10. &lt;php&gt;PDO用法一
  11. ASP.NET MVC 音乐商店 - 目录
  12. 解决android TextView多行文本(超过3行)使用ellipsize属性无效问题
  13. 介绍一款基于jquery好用的编辑框htmlbox.full.js
  14. cat命令汇总整理
  15. 函数计算 Python 连接 SQL Server 小结
  16. AndroidSDK 自带定位工具 uiautomatorviewer
  17. oracle sqlplus操作
  18. Android拦截外拨电话
  19. HDU--4705
  20. python标准库介绍——23 UserString 模块详解

热门文章

  1. 不重启修改&#39;log_slave_updates&#39;变量
  2. seaborn可视化特征的相关性
  3. Tornado 框架的使用
  4. 华为手机如何查看WiFi密码
  5. 生产者消费者模型(Queue,JoinableQueue)
  6. mac mysql中文乱码问题
  7. hive xml udf
  8. leetcode925
  9. 9 python 多态与多态类
  10. vue基础——计算属性和侦听器