안드로이드 스튜디오

[안드로이드 스튜디오 코틀린] 시스템 글씨 크기 키우기

권송미 2023. 4. 21. 08:40
728x90
반응형

 

 

 

 

 

 

 

 

앱을 켰을때, 고정된 글씨 크기의 text를 보여주고 싶었다.

 

방법을 생각해 보았는데

 

1. xml 글씨 크기를 키움

2. 시스템 글씨를 키움

 

등등이 있겠지만, 두 가지의 방법을 테스트해보았다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1. xml 글씨 크기를 키움

 

설정 -> 글자 크기를 제일 작게 설정한 뒤

 

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/testTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="텍스트입니다."
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/fixTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="고정 텍스트 입니다."
        android:textSize="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/testTV" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

둘 다 똑같은 50sp를 주었고 xml을 미리 보기 했을 때 아래처럼 두 개가 같은 글씨 크기입니다.

 

 

하지만 시스템 글씨 크기가 제일 작게 설정이 되어 있어서 앱을 실행한 화면에서는 둘의 크기가 다른 걸 볼 수 있습니다.

 

 

 

 

 

글씨 크기를 최대로 하고 다시 확인해 보았습니다.

 

 

 

 

이렇게 시스템에서 설정을 바꿀 때마다 text 크기가 바뀌는 것을 볼 수 있습니다.

 

 

 

 

 

 

 

 

 

 

 

 

시스템 글씨 크기에 상관없이 화면에 나오는 text의 글씨 크기를 고정시키려면

 

 

 

TypedValue.COMPLEX_UNIT_DIP 를 사용해야 합니다.

 

자세한 정보는 아래의 공식 페이지를 참고하세요.

 

https://developer.android.com/reference/android/util/TypedValue

 

TypedValue  |  Android Developers

 

developer.android.com

 

 

 

 

우선, 뷰바인딩 설정을 추가하였습니다.

 

 

그다음 MainActivity - onCreate에 고정시키고 싶은 글씨 크기를 정하고

setTextSize를 해야 합니다.

 

 

 

        // 글씨 크기 고정
        val font = 50
        binding.fixTV.setTextSize(TypedValue.COMPLEX_UNIT_DIP, font.toFloat())

 

이렇게 두 줄만 추가하면 고정 값의 글씨 크기를 줄 수 있습니다.

 

 

 

 

 

 

 

2. 시스템 글씨를 키움

 

시스템 설정을 위해서 permission을 추가해야 하는데 이 permission은 안드로이드 6 이후로 정책 변경이 되어 시스템 설정에서 허용을 해줘야 시스템 글씨 크기를 바꿔 줄 수 있습니다.

 

 

 

 

 

 

 

 

우선 manifest에 퍼미션을 추가하고

 

<uses-permission android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" />

 

 

 

 

 

 

 

 

 

 

아래 코드를 onCreate에 넣어줍니다.

 

 

 

 

 

        if (!Settings.System.canWrite(this)) {
            val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
            intent.data = Uri.parse("package:$packageName")
            startActivityForResult(intent, 200)
            Settings.System.putFloat(contentResolver, Settings.System.FONT_SCALE, font)
        } else {
            Settings.System.putFloat(contentResolver, Settings.System.FONT_SCALE, font)
        }
    }

 

 

 

앱을 실행하면

 

FontTest 앱의 시스템 허용을 할 수 있는 창이 뜨고 

허용을 해줍니다.

 

 

 

 

 

 

시스템 글씨 크기를 변경한 걸 볼 수 있습니다.

 

 

728x90
반응형