国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android之TextView自適應大小

2019-10-23 19:43:51
字體:
來源:轉載
供稿:網(wǎng)友

對于設置TextView的字體默認大小對于UI界面的好看程度是很重要的,小屏幕設置的文字過大或者大屏幕設置的文字過小都造成UI的不美觀

現(xiàn)在就讓我們學習自適應大小的TextView控件,即當文字長度變化時,文字的大小會相應的變化,保證顯示在一行當中

實現(xiàn)依靠于第三方類庫

第三方類來源:

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

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

效果:

textview自適應大小,textview,文字自適應

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VEVB武林網(wǎng)!


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 尼玛县| 钦州市| 托里县| 社会| 中西区| 宜州市| 长海县| 犍为县| 北碚区| 乐东| 深州市| 桑植县| 丁青县| 冀州市| 靖州| 雅安市| 安龙县| 田东县| 北安市| 洪雅县| 财经| 通州区| 封丘县| 高平市| 博客| 庆城县| 浦城县| 安远县| 驻马店市| 抚顺市| 呼玛县| 炎陵县| 淅川县| 江源县| 高雄市| 淄博市| 邵阳市| 广水市| 德钦县| 赤峰市| 赤峰市|