本篇文章為大家展示了Android中如何設(shè)置TextView的字體默認(rèn)大小,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)從事成都網(wǎng)站制作、成都網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司。作為專(zhuān)業(yè)的建站公司,成都創(chuàng)新互聯(lián)依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營(yíng)經(jīng)驗(yàn),為您提供專(zhuān)業(yè)的成都網(wǎng)站建設(shè)、成都全網(wǎng)營(yíng)銷(xiāo)推廣及網(wǎng)站設(shè)計(jì)開(kāi)發(fā)服務(wù)!
對(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)源:
和正常的使用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
上述內(nèi)容就是Android中如何設(shè)置TextView的字體默認(rèn)大小,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章標(biāo)題:Android中如何設(shè)置TextView的字體默認(rèn)大小
分享路徑:http://aaarwkj.com/article28/ihphcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、域名注冊(cè)、定制開(kāi)發(fā)、云服務(wù)器、動(dòng)態(tài)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)