Android項目實戰(五):TextView自適應大小

來源:互聯網
上載者:User

標籤:action   you   new   show   ide   ack   .com   img   code   

原文:Android項目實戰(五):TextView自適應大小

對於設定TextView的字型預設大小對於UI介面的好看程度是很重要的,小螢幕設定的文字過大或者大螢幕設定的文字過小都造成UI的不美觀

 

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

 

實現依靠於第三方類庫

第三方類來源:

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

 

和正常的使用TextView一樣,只需要將要自適應的TextView標籤設定為<me.grantland.widget.AutofitTextView/>

注意:一定要設定為單行,否定無法顯示效果

android:singleLine="true"

 

 1 <me.grantland.widget.AutofitTextView 2             android:id="@+id/output_autofit" 3             android:layout_width="match_parent" 4             android:layout_height="wrap_content" 5             android:text="@string/example" 6             android:textSize="50sp" 7             android:gravity="center" 8             android:singleLine="true" 9             autofit:minTextSize="8sp"10             />

 

布局檔案:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3             xmlns:autofit="http://schemas.android.com/apk/res-auto" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent"> 6     <LinearLayout 7         android:layout_width="match_parent" 8         android:layout_height="wrap_content" 9         android:orientation="vertical"10         >11         <EditText12             android:id="@+id/input"13             android:layout_width="match_parent"14             android:layout_height="wrap_content"15             android:singleLine="true"16             android:hint="@string/input_hint"17             android:text="@string/example"/>18         <TextView19             android:layout_width="match_parent"20             android:layout_height="wrap_content"21             android:text="@string/label_normal"22             />23         <TextView24             android:id="@+id/output"25             android:layout_width="match_parent"26             android:layout_height="wrap_content"27             android:text="@string/example"28             android:textSize="50sp"29             android:gravity="center"30             />31         <TextView32             android:layout_width="match_parent"33             android:layout_height="wrap_content"34             android:text="@string/label_autofit"35             />36         <me.grantland.widget.AutofitTextView37             android:id="@+id/output_autofit"38             android:layout_width="match_parent"39             android:layout_height="wrap_content"40             android:text="@string/example"41             android:textSize="50sp"42             android:gravity="center"43             android:singleLine="true"44             autofit:minTextSize="8sp"45             />46     </LinearLayout>47 </ScrollView>
activity_main.xml

string.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3  4     <string name="app_name">Texttest</string> 5     <string name="action_settings">Settings</string> 6     <string name="hello_world">Hello world!</string> 7  8     <string name="input_hint">text</string> 9     <string name="label_normal">Normal:</string>10     <string name="label_autofit">Autofit:</string>11 12     <string name="example">This is an example</string>13 14 </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;10 11 public class MainActivity extends Activity {12 13     private TextView mOutput;14     private TextView mAutofitOutput;15     @Override16     protected void onCreate(Bundle savedInstanceState) {17         super.onCreate(savedInstanceState);18         setContentView(R.layout.activity_main);19         mOutput = (TextView)findViewById(R.id.output);20         mAutofitOutput = (TextView)findViewById(R.id.output_autofit);21 22         ((EditText)findViewById(R.id.input)).addTextChangedListener(new TextWatcher() {23             @Override24             public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {25                 // do nothing26             }27 28             @Override29             public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {30                 mOutput.setText(charSequence);31                 mAutofitOutput.setText(charSequence);32             }33 34             @Override35             public void afterTextChanged(Editable editable) {36                 // do nothing37             }38         });39     }40     41 42 43     @Override44     public boolean onCreateOptionsMenu(Menu menu) {45         // Inflate the menu; this adds items to the action bar if it is present.46         getMenuInflater().inflate(R.menu.main, menu);47         return true;48     }49     50 }
MainActivity.java

效果:

 

Android項目實戰(五):TextView自適應大小

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.