Android之TextView自適應(yīng)大小
對(duì)于設(shè)置TextView的字體默認(rèn)大小對(duì)于UI界面的好看程度是很重要的,小屏幕設(shè)置的文字過(guò)大或者大屏幕設(shè)置的文字過(guò)小都造成UI的不美觀
現(xiàn)在就讓我們學(xué)習(xí)自適應(yīng)大小的TextView控件,即當(dāng)文字長(zhǎng)度變化時(shí),文字的大小會(huì)相應(yīng)的變化,保證顯示在一行當(dāng)中
實(shí)現(xiàn)依靠于第三方類(lèi)庫(kù)
第三方類(lèi)來(lái)源:
https://github.com/grantland/android-autofittextview
和正常的使用TextView一樣,只需要將要自適應(yīng)的TextView標(biāo)簽設(shè)置為<me.grantland.widget.AutofitTextView/>
注意:一定要設(shè)置為單行,否定無(wú)法顯示效果
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
效果:
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
解決Android webview設(shè)置cookie和cookie丟失的問(wèn)題
這篇文章主要介紹了解決Android webview設(shè)置cookie和cookie丟失的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android使alertDialog.builder不會(huì)點(diǎn)擊外面和按返回鍵消失的方法
本篇文章主要介紹了Android使alertDialog.builder不會(huì)點(diǎn)擊外面和按返回鍵消失的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01Android okhttp的啟動(dòng)流程及源碼解析
這篇文章主要介紹了Android okhttp的啟動(dòng)流程及源碼解析,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-03-03Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法
這篇文章主要介紹了Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法,通過(guò)ListView控件綁定setOnScrollListener方法簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的功能,需要的朋友可以參考下2016-01-01Android中Fragmen首選項(xiàng)使用自定義的ListPreference的方法
Android中Fragmen的首選項(xiàng)可以使用自定義的ListPreference,這樣Fragment的PreferenceFragment就可以更方便地保存配置信息,需要的朋友可以參考下2016-05-05Android編程簡(jiǎn)單實(shí)現(xiàn)雷達(dá)掃描效果
這篇文章主要介紹了Android編程簡(jiǎn)單實(shí)現(xiàn)雷達(dá)掃描效果,涉及Android圖形繪制及顯示的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
這篇文章主要介紹了android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android布局之GridLayout網(wǎng)格布局
網(wǎng)格布局標(biāo)簽是GridLayout。這個(gè)布局是android4.0新增的布局。這個(gè)布局只有4.0之后的版本才能使用。本文給大家介紹Android布局之GridLayout網(wǎng)格布局相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2015-12-12