AutosizingTextView について
AutosizingTextView の詳細については以前投稿したもを参考にしてください
画面の表示サイズ(dpi)変更に耐えられるLayoutを定義する - Qiita
AutosizingTextView を APIレベル 26 未満で使う
AutosizingTextView は Android 8.0(API レベル 26)以降で使用できるようになった機能である
今回の目的は APIレベル 26 未満でも AutosizingTextView の自動サイズが適用される Layout を投稿することである
APIレベル 26 未満でも簡単に使用できたのでメモしておく
普通の TextView でのレイアウト崩れの例
・3行に改行されてしまうTextViewがあったとする
・ここの文字列は可変であり何がくるかわからないとする(3文字くらいの単語が入ることも想定)
・短い単語では表示に問題はない
・横幅は変えれないような仕様となっている
・長い文字列の時に表示が崩れたくない要望がある
どんな文字列が来ても表示が大丈夫なように作っておきたい
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/view1"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Hello World"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/button1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AutosizingTextView で解決
AutosizingTextView を使うといい感じに1行で自動的に収まるようになる
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/view1"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="20sp"
android:autoSizeMinTextSize="1dp"
android:autoSizeTextType="uniform"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="Hello World"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/button1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
APIレベル 26 未満では適用されない
上のレイアウトと同じ実装で Andord 6 といった APIレベル 26 未満のOSでアプリを見てみるとリサイズされていない
Android 6 や 7 のシェアもまあまあ高いのでなんとかしたい
Support Library で解決
Support Library を使うだけで下位のOSでも自動でリサイズできるようになる
今は AndroidX に Support Library が含まれるので AndroidX で定義してあげたほうが望ましい
https://developer.android.com/topic/libraries/support-library
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/view1"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- android.support.v7.widget.AppCompatTextView -->
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="Hello World"
android:textSize="20sp"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="1dp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/button1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
※ 「android:」 で定義されているところを 「app:」 に変えないとリサイズ効果が出ないので注意すること