字体不随系统的字体大小变化而变化

一、 APP字体大小,不随系统的字体大小变化而变化的方法

1、将字体大小的单位设置了dp,就可以固定字体大小不随系统设定的字号变化

sp和dp很类似但唯一的区别是,Android系统允许用户自定义文字尺寸大小(小、正常、大、超大等等),当文字尺寸是“正常”时1sp=1dp=0.00625英寸,而当文字尺寸是“大”或“超大”时,1sp>1dp=0.00625英寸。

2、代码设置(新)

● 新建类MyContextWrapper

 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.os.Build;
 import android.support.annotation.NonNull;
 import android.util.DisplayMetrics;
 
 public class MyContextWrapper extends ContextWrapper { 
     public MyContextWrapper(Context base) {
         super(base);
     }
     @NonNull
     public static ContextWrapper wrap(Context context) {
         Resources resources = context.getResources();
         Configuration newConfig = new Configuration();
         DisplayMetrics metrics = resources.getDisplayMetrics();
         newConfig.setToDefaults();
         //如果没有设置densityDpi, createConfigurationContext对字体大小设置限制无效
        newConfig.densityDpi = metrics.densityDpi;
         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
             context = context.createConfigurationContext(newConfig);
         } else {
            resources.updateConfiguration(newConfig, resources.getDisplayMetrics());
         }
        return new MyContextWrapper(context);
     }
 }
复制代码

● 在所有Activity(BaseActivity)添加

@Override
 protected void attachBaseContext(Context newBase) {
     super.attachBaseContext(MyContextWrapper.wrap(newBase));
 }
复制代码

updateConfiguration 设置会对其他Activity也有效。 createConfigurationContext 自对当前Activity有效。

3、代码设置(过时)

1)在Application中加入

private void setTextDefault() {
     Resources res = super.getResources();
     Configuration config = new Configuration();
     config.setToDefaults();
     res.updateConfiguration(config, res.getDisplayMetrics());
 }
复制代码

缺点:如果home出来,更改了字体大小,字体还是会改变。完全退出应用在进去,字体才会改为默认大小。

2)在所有Activity 中加入,改变字体大小能及时还原默认大小。

@Override
 public Resources getResources() {
  Resources resources = super.getResources();
  if (resources.getConfiguration().fontScale != 1) {
     Configuration newConfig = new Configuration();
     newConfig.setToDefaults();
     resources.updateConfiguration(newConfig, resources.getDisplayMetrics());
  }
  return resources;
 }
复制代码

二、WebView 显示html 字体大小不随系统变化

webSettings.setTextZoom(100);//防止系统字体大小影响布局


作者:wangru0926
链接:https://juejin.im/post/5c9c6ec66fb9a070f90ace36
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

在Android开发中,如果你希望应用的字体大小系统字体大小变化变化,可以通过以下几种方法来实现: 1. **使用dp单位**:在布局文件中使用dp(density-independent pixels)单位来设置字体大小,而是使用sp(scale-independent pixels)单位。dp单位会随着系统字体大小变化变化。 ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="固定字体大小" android:textSize="16dp" /> ``` 2. **代码中设置字体大小**:在代码中使用setTextSize方法,并传入TypedValue.COMPLEX_UNIT_DIP来设置字体大小。 ```java TextView textView = findViewById(R.id.textView); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); ``` 3. **自定义TextView**:创建一个自定义的TextView类,重写setTextSize方法,使其系统字体大小变化。 ```java public class FixedTextView extends androidx.appcompat.widget.AppCompatTextView { public FixedTextView(Context context) { super(context); } public FixedTextView(Context context, AttributeSet attrs) { super(context, attrs); } public FixedTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void setTextSize(float size) { super.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size); } @Override public void setTextSize(int unit, float size) { if (unit == TypedValue.COMPLEX_UNIT_DIP) { super.setTextSize(unit, size); } else { super.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size); } } } ``` 在布局文件中使用自定义的TextView: ```xml <com.example.yourapp.FixedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="固定字体大小" android:textSize="16dp" /> ``` 通过以上方法,你可以确保应用中的字体大小会随着系统字体大小变化变化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值