android 设置默认字体大小,Android中如何设置TextView的字体默认大小

Android中如何设置TextView的字体默认大小

发布时间:2020-11-25 16:46:14

来源:亿速云

阅读:81

作者:Leah

本篇文章为大家展示了Android中如何设置TextView的字体默认大小,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

对于设置TextView的字体默认大小对于UI界面的好看程度是很重要的,小屏幕设置的文字过大或者大屏幕设置的文字过小都造成UI的不美观

现在就让我们学习自适应大小的TextView控件,即当文字长度变化时,文字的大小会相应的变化,保证显示在一行当中

实现依靠于第三方类库

第三方类来源:

和正常的使用TextView一样,只需要将要自适应的TextView标签设置为

注意:一定要设置为单行,否定无法显示效果

android:singleLine="true"

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"

/>

布局文件:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

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"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/label_normal"

/>

android:id="@+id/output"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/example"

android:textSize="50sp"

android:gravity="center"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/label_autofit"

/>

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"

/>

activity_main.xml

string.xml

Texttest

Settings

Hello world!

text

Normal:

Autofit:

This is an example

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

上述内容就是Android中如何设置TextView的字体默认大小,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值