当前位置: 移动技术网 > IT编程>移动开发>Android > Android之TextView自适应大小

Android之TextView自适应大小

2019年07月24日  | 移动技术网IT编程  | 我要评论

故宫卫星地图,群发的短信我不回,南宁金桥客运站

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

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

实现依靠于第三方类库

第三方类来源:

和正常的使用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

效果:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网