LoginSignup
32
15

More than 3 years have passed since last update.

Android Pie対応するときにTextViewの改行の行間でハマった話

Last updated at Posted at 2019-05-26

targetSdkVersionを28にした

Android 9 へのアプリの移行 を読んで
compileSdkVersionとtargetSdkVersionを28に変更してビルド実行、ビルド通ったし動作確認するかー
と動作確認していたときに「android:lineSpacingMultiplier」を設定している箇所を開いた際に
アレ?行間広くなってね?
と思って調べたときにハマったので記載

問題の表示

実際の表示を確認するためにTextViewのHello Worldを改行付き文字に置き換えただけの
Empty Activityで作成したプロジェクトを用意し検証

Android 8のEmulator Android 9のEmulator

TextViewの内容

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="君がッ\n泣くまで\n殴るのを\nやめないッ!"
    android:lineSpacingMultiplier="1.5"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

明らかに行間が開いている!

不具合では?思って調査してみると…

調査してみるとgoogle issue trackerに似たような話が…
どうやら不具合ではなく想定通りの動作らしく
fallbackLineSpacingをfalseにすれば戻ると言う事なので実際に試してみる

google issue tracker
https://issuetracker.google.com/issues/122554779

※Android DevelopersのfallbackLineSpacingの記載箇所
https://developer.android.com/reference/android/widget/TextView.html#attr_android:fallbackLineSpacing

実際に試してみた結果

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="君がッ\n泣くまで\n殴るのを\nやめないッ!"
    android:lineSpacingMultiplier="1.5"
    android:fallbackLineSpacing="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
Android 8のEmulator Android 9のEmulator

android:fallbackLineSpacingにfalseを設定すると行間が同じになった!

関係ありそうな箇所を見てみる

fallbackLineSpacingの箇所をみてみるとこのようなコードになっていたので
PieからデフォルトがfallbackLineSpacingがtrueになる様子

mUseFallbackLineSpacing = context.getApplicationInfo().targetSdkVersion >= VERSION_CODES.P;

fallbackLineSpacingの説明をGoogle先生の翻訳にかけてみると下記の内容になるのでtrueが設定されている場合には行間が増えてしまうっぽい?

翻訳後の内容
「テキストの表示に使用される代替フォントの上昇および下降を尊重するかどうか。trueの場合、使用されてしまうフォールバックフォントは、使用されている行の上昇および下降を増やす可能性があります。」

Android 9 へのアプリの移行を読んだけど…

今回この事象にぶつかってからどうしたら解決できるのか情報を漁り、この方法を発見したが見つけるまで苦労した…
この辺りの事は特に記載が無い気がする(見逃しているだけ? or 自分が無知なだけ?)
android:fallbackLineSpacingにfalseを設定すれば行間を合わせられそうだが…他の方法とかあるんだろうか…

32
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
15