IT박스

기본 설정에서 글꼴 크기를 변경하면 Android 애플리케이션에서 TextView의 글꼴 크기가 변경됩니다.

itboxs 2020. 6. 9. 22:23
반응형

기본 설정에서 글꼴 크기를 변경하면 Android 애플리케이션에서 TextView의 글꼴 크기가 변경됩니다.


응용 프로그램에서 자체 텍스트 크기를 지정하고 싶지만이 작업을 수행하는 데 문제가 있습니다.

장치 설정에서 글꼴 크기를 변경하면 응용 프로그램의 글꼴 크기 TextView도 변경됩니다.


실제로 설정 글꼴 크기는의 크기에만 영향을줍니다 sp. 정의 - 모든 그래서 당신은 할 필요가 textSize있는 dp대신에 sp, 다음 설정 앱에서 텍스트 크기를 변경하지 않습니다.

여기에 문서에 대한 링크는 다음과 같습니다 치수

그러나 예상되는 동작은 모든 앱의 글꼴이 사용자의 기본 설정을 존중한다는 것입니다. 사용자가 글꼴 크기를 조정하려는 이유는 여러 가지가 있으며 그 중 일부는 시각 장애가있는 의료 사용자 일 수도 있습니다. 텍스트 dp대신에 사용하면 sp일부 앱 사용자를 기꺼이 차별 할 수 있습니다.

즉 :

android:textSize="32dp"

가장 쉬운 방법은 다음과 같은 것을 사용하는 것입니다.

android:textSize="32sp"

textSize속성 에 대한 자세한 내용을 보려면 Android 개발자 설명서를 확인하십시오 .


dimension리소스를 사용하는 것처럼 리소스 유형을 사용하십시오 string( DOCS ).

당신의에서 dimens.xml파일, 당신의 치수 변수를 선언 :

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="textview_height">25dp</dimen>
  <dimen name="textview_width">150dp</dimen>
  <dimen name="ball_radius">30dp</dimen>
  <dimen name="font_size">16sp</dimen>
</resources>

그런 다음이 값을 다음과 같이 사용할 수 있습니다.

<TextView
   android:layout_height="@dimen/textview_height"
   android:layout_width="@dimen/textview_width"
   android:textSize="@dimen/font_size"/>

dimens.xml다른 유형의 화면에 대해 다른 파일을 선언 할 수 있습니다 . 이렇게하면 다른 기기에서 원하는 앱 모양을 보장 할 수 있습니다.

지정하지 않으면 android:textSize시스템이 기본값을 사용합니다.


또한 textSize가 코드로 설정된 경우 호출 textView.setTextSize(X)하면 숫자 (X)가 SP로 해석됩니다. dp의setTextSize(TypedValue.COMPLEX_UNIT_DIP, X) 값을 설정하는 데 사용하십시오 .


dimen.xml 파일에 이미 정의되어있을 때 코드로 DIP 또는 SP를 다시 지정하는 것은 좋지 않습니다.

가장 좋은 옵션은 dimen.xml 값을 사용할 때 PX를 사용하는 것입니다.

tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_size));

이렇게하면 dimen.xml 파일에서 코드를 변경하지 않고도 DP에서 SP로 전환 할 수 있습니다.


전체 앱이 시스템 글꼴 크기에 영향을받지 않도록하는 간단한 방법은 기본 활동을 사용하여 구성을 업데이트하는 것입니다.

//in base activity add this code.
public  void adjustFontScale( Configuration configuration) {

    configuration.fontScale = (float) 1.0;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = configuration.fontScale * metrics.density;
    getBaseContext().getResources().updateConfiguration(configuration, metrics);

}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adjustFontScale( getResources().getConfiguration());
}

Using font size in DPs instead of SPs in textSize solves this.


this may help. add the code in your custom Application or BaseActivity

/**
 * 重写 getResource 方法,防止系统字体影响
 *
 * @return
 */
@Override
public Resources getResources() {
    Resources resources = super.getResources();
    if (resources != null && resources.getConfiguration().fontScale != 1) {
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = 1;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }
    return resources;
}

however, Resource#updateConfiguration is deplicated in API level 25, which means it will be unsupported some day in the future.


You have to write in TextView Tags in XML :

android:textSize="32dp"

If units used are SP, not DP, and you want to override system font scaling, all you do is override one method in activity - see this post.

resources.updateConfiguration is deprecated and buggy (on Oreo I had issues with formatted text).


You can use this code:

android:textSize="32dp"

it solves your problem but you must know that you should respect user decisions. by this, changing text size from device settings will not change this value. so that's why you need to use sp instead of dp. So my suggestion is to review your app with different system font sizes(small,normal,big,...)


Override getResources() in Activity.

Set TextView's size in sp as usual like android:textSize="32sp"

override fun getResources(): Resources {
    return super.getResources().apply {
        configuration.fontScale = 1F
        updateConfiguration(configuration, displayMetrics)
    }
}

참고URL : https://stackoverflow.com/questions/13264794/font-size-of-textview-in-android-application-changes-on-changing-font-size-from

반응형