Android Project Combat (V): TextView Adaptive size

Source: Internet
Author: User

Original: Android Project Combat (V): TextView Adaptive size

The default size of the font set for the TextView is important for the UI interface to look good, the small screen setting of the text is too large or large screen settings of the text too small to cause the UI is not beautiful

Now let's learn the adaptive size of the TextView control, that is, when the length of the text changes, the size of the text will change accordingly, guaranteed to display in one line

Implementation relies on third-party class libraries

Third-party class sources:

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

As with normal use of TextView, only the TextView label that will be adaptive is set to <me.grantland.widget.AutofitTextView/>

Note: Be sure to set as a single line, negation cannot show effect

Android:singleline="true"

1<Me.grantland.widget.AutofitTextView2Android:id="@+id/output_autofit"3Android:layout_width="match_parent"4android:layout_height="wrap_content"5android:text="@string/example"6Android:textsize="50SP"7android:gravity="Center"8Android:singleline="true"9Autofit:mintextsize="8SP"Ten/>

Layout file:

1<?xml version="1.0"encoding="Utf-8"?>2<scrollview xmlns:android="http://schemas.android.com/apk/res/android"3xmlns:autofit="Http://schemas.android.com/apk/res-auto"4Android:layout_width="match_parent"5android:layout_height="match_parent">6<LinearLayout7Android:layout_width="match_parent"8android:layout_height="wrap_content"9android:orientation="Vertical"Ten> One<EditText AAndroid:id="@+id/input" -Android:layout_width="match_parent" -android:layout_height="wrap_content" theAndroid: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" A/> at<TextView -Android:id="@+id/output" -Android:layout_width="match_parent" -android:layout_height="wrap_content" -android:text="@string/example" -Android:textsize="50SP" inandroid:gravity="Center" -/> to<TextView +Android:layout_width="match_parent" -android:layout_height="wrap_content" theandroid:text="@string/label_autofit" */> $<Me.grantland.widget.AutofitTextViewPanax NotoginsengAndroid:id="@+id/output_autofit" -Android:layout_width="match_parent" theandroid:layout_height="wrap_content" +android:text="@string/example" AAndroid:textsize="50SP" theandroid:gravity="Center" +Android:singleline="true" -Autofit:mintextsize="8SP" $/> $</LinearLayout> -</ScrollView>
Activity_main.xml

String.xml

1<?xml version="1.0"encoding="Utf-8"?>2<resources>3 4<stringName="app_name">texttest</string>5<stringName="action_settings">settings</string>6<stringName="Hello_world">hello world!</string>7 8<stringName="Input_hint">text</string>9<stringName="Label_normal">normal:</string>Ten<stringName="Label_autofit">autofit:</string> One  A<stringName="Example">this isAn example</string> -  -</resources>
View Code

Activity

1 Package com.example.texttest;2 3 import android.app.Activity;4 import Android.os.Bundle;5 import android.text.Editable;6 import Android.text.TextWatcher;7 import Android.view.Menu;8 import Android.widget.EditText;9 import Android.widget.TextView;Ten  One  Public classMainactivity extends Activity { A  -     PrivateTextView moutput; -     PrivateTextView mautofitoutput; the @Override -     protected voidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +Moutput =(TextView) Findviewbyid (r.id.output); -Mautofitoutput =(TextView) Findviewbyid (r.id.output_autofit); +  A((EditText) Findviewbyid (R.id.input)). Addtextchangedlistener (NewTextwatcher () { at @Override -              Public voidBeforetextchanged (Charsequence charsequence,intIintI2,inti3) { -                 // do nothing -             } -  - @Override in              Public voidOnTextChanged (Charsequence charsequence,intIintI2,inti3) { - Moutput.settext (charsequence); to Mautofitoutput.settext (charsequence); +             } -  the @Override *              Public voidaftertextchanged (Editable Editable) { $                 // do nothingPanax Notoginseng             } -         }); the     } +      A  the  + @Override -      Publicboolean 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; -     } the      -}
Mainactivity.java

Effect:

Android Project Combat (V): TextView Adaptive size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.