android_compose_Text_のフォントサイズを直接数値で指定

 


androidのアプリ作成について学習しているのですが、

composeで UIを作成する場合に、


Text(

text = feCalc.calcStrOp,

modifier = Modifier

.width(w * 0.10)

.height(h *0.07),

fontSize = val_font_size_box_s.sp

)



上記のようなTextを使うとき、

フォントサイズを 変数ではなく

英語だと hard coded value というのでしょうか?


そういうような直接数値をspとして、あるいはpxとして指定する方法は

あるのでしょうか?




Yes, in Jetpack Compose, you can specify a font size 

as a hardcoded value directly in `sp` or `px`.


You don't necessarily need a variable. 



Here’s how you could specify it directly:



```kotlin


Text(


    text = feCalc.calcStrOp,

    modifier = Modifier

        .width(w * 0.10f)

        .height(h * 0.07f),

    fontSize = 16.sp // Here, `16.sp` is a hardcoded font size

)


```


Or, if you want to use pixels directly, use `.withFontSize()`. 


Since Compose uses `sp` as the standard for text, 

consider staying with `sp` for text scaling.


So, when you hardcode it directly like `16.sp`, 

it's indeed referred to as a "hardcoded value." 


This practice is fine for testing or prototypes 

but might make it harder to adapt for different screen sizes 

if not designed responsively.


コメント